Home  >  Article  >  Backend Development  >  Teach you step by step to create a php extension (basic steps)

Teach you step by step to create a php extension (basic steps)

不言
不言Original
2018-08-28 11:57:101821browse

The content of this article is about teaching you step by step to create a php extension (basic steps). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What are the basic steps to create an extension. In the example, we will implement the following functions:

<?php
echo say();
?>

Output content:

$ php ./test.php
$ hello word

Implement a say method in the extension, and after calling the say method, output the hello word.

Step one: Generate code

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

$ cd php_src/ext/
$ ./ext_skel --extname=say

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

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])

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, uncomment the PHP_ARG_ENABLE related code section. 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 ; comment is aligned:
[ --enable-say                 Enable say support])

The third step is to implement the code

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

PHP_FUNCTION(say)
{
    zend_string *strg;
    strg = strpprintf(0, "hello word");
    RETURN_STR(strg);
}
Find PHP_FE(confirm_say_compiled), add the following code above it:

PHP_FE(say, NULL)

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[] */
 };
/ }}} /

The fourth step, compile and install

The steps to compile the extension are as follows:

$ phpize
$ ./configure
$ make && make install

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

[say]
extension = say.so

Then execute, 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. See if the output content conforms to Expected.

Related recommendations:

[Transfer] Write your own PHP extension to create a class - orlion

Create one from scratch PHP extension

The above is the detailed content of Teach you step by step to create a php extension (basic steps). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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