This article is to bring you the content is about to teach you a step-by-step to create a PHP extension (basic steps), there is a certain reference value, the need for friends can refer to, I hope to help you.
What are the basic steps for creating an extension? example, we will implement the following functions:
<?phpecho say ();? >
Output content:
$ php./test.php$ Hello Word
Implement a say method in the extension, and after calling the say method, output Hello Word.
First step: Generate code
PHP provides us with the tool Ext_skel to generate the codebase. This tool is in the PHP source code under the./ext directory.
$ 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 will be generated under the current directory.
Step two, modify the CONFIG.M4 configuration file
The role of CONFIG.M4 is to generate configure files with the Phpize tool. The Configure file is used for environmental testing. Detects if the environment required for the extended compilation run is satisfied. Now we begin to modify the Config.m4 file.
$ CD./say$ vim./config.m4
After opening, CONFIG.M4 file, you will find such a paragraph of text.
DNL If your extension references something external, use with:
DNL Php_arg_with (say, for say support,
DNL Make sure, 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, the comment is aligned:
DNL [--enable-say enable say support])
Where dnl is an annotation symbol. The code above says that if you write an extension that relies on other extensions or LIB libraries, you need to remove the comments from the Php_arg_with related code. Otherwise, remove the comment for the php_arg_enable related code snippet. The extensions we write do not need to rely on other extensions and LIB libraries. Therefore, we remove the comments in front of the php_arg_enable. The code after removing the comment is as follows:
DNL If your extension references something external, use with:
DNL Php_arg_with (say, for say support,
DNL Make sure, 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, the comment is aligned:
[--enable-say enable say support])
The third step, the code implementation
Modify the Say.c file. Implement the Say method.
Locate Php_function (confirm_say_compiled) and add the following code on top of 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:
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_co mpiled, NULL)/ * For testing, remove later. */php_fe_end */must be the last line in say_functions[] */};/} }} /
Fourth step, compile and install
The steps to compile the extension are as follows:
$ phpize$./configure$ make && make install
Modify the php.ini file to add the following code:
[say]extension = say.so
Then execute the php-m command. In the content of the output, you will see the say words.
Fifth step, call the test
Write a script yourself, calling the Say method. See if the output matches the expected content.