Objective
This is a long time to write a memo, writing PHP extension 100 degrees are articles, but a lot of articles are very old. Some of the examples are not running through. A little awkward.
This article is used to record your own notes as a memo.
Body
1. Download the PHP installation package
Download address: PHP Download fast Chain
This article selects the php-5.6.7 installation package.
Then install PHP.
2. Create an extended skeleton
Run to ext directory CD php-5.6.7/ext///perform one-click operation to build the skeleton./ext_skel--extname=helloworld
If you see the following tips for creating results
CD Helloworldls
You will find the following files:
CONFIG.M4 config.w32 CREDITS experimental helloworld.c helloworld.php php_helloworld.h Tests
3. Modify the extended configuration file config.m4
Remove the DNL before the code below. (DNL is equivalent to PHP//)
# #动态编译选项, link via. So, remove DNL comment php_arg_with (HelloWorld, for HelloWorld Support,make sure that the comment is aligned:[ --with-helloworld Include HelloWorld support] # #静态编译选项, enabled by enable, remove DNL annotations php_arg_enable (HelloWorld, whether To enable HelloWorld Support,make Sure, the comment is aligned:[ --enable-helloworld enable HelloWorld Suppor T])
In general, choose one (see a person's preference, this tutorial must remove the Enable comment).
4. Compile the test
Phpize./configure--enable-helloworldmakemake Install
Then add the extension to php.ini
vim/usr/local/php/etc/php.ini//Add extension extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/ "Extension =" helloworld.so "//restart PHP-FPM/ETC/INIT.D/PHP-FPM restart
Go back to the Write extension folder and execute the test command
php-d Enable_dl=on myfile.php
See the following words stating that the victory is not far away:
confirm_helloworld_compiledcongratulations! You have successfully modified EXT/HELLOWORLD/CONFIG.M4. Module HelloWorld is now compiled to PHP.
Confirm_helloworld_compiled is a test function that Ext_skel automatically generates.
PS: If you install two PHP versions locally, and you are writing extensions with PHP7, you may encounter the following issues:
/MYDATA/SRC/PHP-7.0.0/EXT/HELLOWORLD/HELLOWORLD.C: In function ' zif_confirm_helloworld_compiled ':/mydata/src/php-7.0.0/ EXT/HELLOWORLD/HELLOWORLD.C:58: Error: ' zend_string ' is not declared (first used within this function)/mydata/src/php-7.0.0/ext/helloworld/ HELLOWORLD.C:58: Error: (even if multiple occurrences occur within a function, each undeclared identifier is in its/mydata/src/php-7.0.0/ext/helloworld/helloworld.c:58: Error: Within the function is also reported only once. )/mydata/src/php-7.0.0/ext/helloworld/helloworld.c:58: Error: ' STRG ' not declared (first use within this function)
Cause: The compiled environment is not PHP7.
Workaround: php5 There is no zend_string type, replace with char, or modify your PHP version environment to PHP7
5. Create the HelloWorld function
Edit the HELLOWORLD.C to supplement the function to be implemented
# #zend_function_entry Helloworld_functions Supplement to implement the function const Zend_function_entry helloworld_functions[] = { Php_fe ( confirm_helloworld_compiled, NULL)/ * For testing, remove later. * /Php_fe (HelloWorld, null)/ * This is a supplementary line, the tail does not have a comma */php_fe_end/* Must is the last lines in helloworld_functions[] */};
Find "Php_function (confirm_helloworld_compiled)" and another function to write the function entity:
Php_function (HelloWorld) { php_printf ("Hello world!\n"); Return_true;}
Go through the compilation again:
./configure--enable-helloworld && make && make install
Test if it is really successful:
php-d enable_dl=on-r "DL (' helloworld.so '); HelloWorld ();" Output Hello world!
Success!