Detailed explanation of hello word implementation method in PHP7 extension development

jacklove
Release: 2023-04-02 07:54:01
Original
1607 people have browsed it

This article mainly introduces the implementation method of hello word in PHP7 extension development. It analyzes the specific steps and related operating techniques of PHP7 extension development in the form of examples. It involves the modification and compilation of the underlying source code of PHP. Friends in need can refer to the following

The example in this article describes the implementation method of hello word in PHP7 extension development. Share it with everyone for your reference, the details are as follows:

Here is based on PHP7, explaining how to create a PHP extension from scratch. This article mainly explains the basic steps to create an extension. In the example, we will implement the following functions:

<?php
echo say();
?>
Copy after login

Output content:

$ php ./test.php
$ hello word
Copy after login

Implement a say method in the extension. After calling the say method, the hello word is output.

Step one: Generate code

PHP provides us with a tool to generate basic code ext_skel. This tool is in the ./ext directory of the PHP source code.

$ cd php_src/ext/
$ ./ext_skel --extname=say
Copy after login

The value of the extname parameter is the extension name. After executing the ext_skel command, a directory with the same extension name will be generated in the current directory.

The second step is to modify the config.m4 configuration file

The function of config.m4 is to cooperate with the phpize tool to generate the configure file. The configure file is used for environment detection. Check whether the environment required for extension compilation and running is met. Now we start to modify the config.m4 file.

$ cd ./say
$ vim ./config.m4
Copy after login

After opening the config.m4 file, you will find this paragraph.

dnl If your extension references something external, use with:
dnl PHP_ARG_WITH(say, for say support,
dnl Make sure that the comment is aligned:
dnl [ --with-say       Include say support])
dnl Otherwise use enable:
dnl PHP_ARG_ENABLE(say, whether to enable say support,
dnl Make sure that the comment is aligned:
dnl [ --enable-say      Enable say support])
Copy after login

Among them, dnl is the comment symbol. The above code says that if the extension you write depends on other extensions or lib libraries, you need to remove the comments on the PHP_ARG_WITH related code. Otherwise, remove the comments from the PHP_ARG_ENABLE relevant code segment. The extensions we write do not need to rely on other extensions and lib libraries. Therefore, we remove the comment in front of PHP_ARG_ENABLE. The code after removing the comments is as follows:

dnl If your extension references something external, use with:
 dnl PHP_ARG_WITH(say, for say support,
 dnl Make sure that the comment is aligned:
 dnl [ --with-say       Include say support])
 dnl Otherwise use enable:
 PHP_ARG_ENABLE(say, whether to enable say support,
 Make sure that the comment is aligned:
 [ --enable-say      Enable say support])
Copy after login

The third step, code implementation

Modify the say.c file. Implement the say method.
FindPHP_FUNCTION(confirm_say_compiled), add the following code above it:

PHP_FUNCTION(say)
{
    zend_string *strg;
    strg = strpprintf(0, "hello word");
    RETURN_STR(strg);
}
Copy after login

FindPHP_FE(confirm_say_compiled, add the following code above:

PHP_FE(say, NULL)
Copy after login

The modified code is as follows:

const zend_function_entry say_functions[] = {
   PHP_FE(say, NULL)    /* For testing, remove later. */
   PHP_FE(confirm_say_compiled,  NULL)    /* For testing, remove later. */
   PHP_FE_END /* Must be the last line in say_functions[] */
 };
 /* }}} */
Copy after login

The fourth step, compile and install

The steps to compile the extension are as follows:

$ phpize
$ ./configure
$ make && make install
Copy after login

Modify the php.ini file and add the following code:

[say]
extension = say.so
Copy after login

Then execute the php -m command. In the output content, You will see the word say.

The fifth step is to call the test

Write a script yourself and call the say method. Look at the output Is it in line with expectations?

Articles you may be interested in:

Detailed explanation of the method of using lib library based on function in PHP extension development

The particularity of the volist tag in thinkphp in ajax operations

Detailed explanation of the volist tag in thinkphp

The above is the detailed content of Detailed explanation of hello word implementation method in PHP7 extension development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template