This article mainly introduces you to write PHP extension from scratch, we will start from how to generate a php extension, we hope to help everyone.
PHP is written in C language. For each phper, there is a desire to write the expansion of the heart. However, there is a lack of a good entry point. Google on the search for PHP extension development, most of them are reproductions of articles, and even some people do not even operate the operation on their own blog. But there are a few good tutorials, but all are the product of the PHP 5 era, hiding a lot of pits. I will take my own step down the process of logging down, perhaps this is the other people's "tutorial" it.
Generate an extension
Presumably a lot of people have seen a lot of online tutorials. Most of them teach us to execute this command: $./ext_skel--extname=extname. However, when you clone the PHP source, you will find that the master branch does not ext/ext_skel the file. So, I summed up a bit:
If you are directly downloading the source code of PHP, or under the release of the version, you can execute this command
$ cd ext$./ext_skel--extname=extname
If you are directly under the Master branch, only the ext_skel.php file, this time you will be able to execute this PHP file directly
$ cd ext$ php ext_skel.php--ext extname
Since I developed directly under the Master branch, the following are the default actions under Master.
After the extension is generated, we will see four files and a folder. At this stage, we only need to use two files,. c files and. h files.
A little Pit
After we've built a good extension, we can try to compile
$ phpize$./configure$ make &&make Test
We will be surprised to find that there is a warning when compiling.
Warning:implicitdeclaration of function ' Zend_parse_parameters_none ' isinvalid inC99 [- Wimplicit-function-declaration]zend_parse_parameters_none (); ^
1warning generated.
Then you execute make test and find that one of the tests did not pass. Yes, the script generates good files for us, but it can't even pass our own tests. Have you ever felt so weird? Let's take a look at warning's specific information. The function Zend_parse_parameters_none could not be found. Looked at the file and found it on line 15th. Look at the function name to guess what it means. So I went to the PHP source to search for a bit. But we found such a macro definition.
#ifndefzend_parse_parameters_none #definezend_parse_parameters_none () zend_parse_parameters (Zend_num_args (), "") # endif
After replacing the original capitalization, there is no warning. This is the official to dig a small hole for us. Although there is a macro definition of uppercase, but why the error, I am not very clear.
Define a function
I think most people write extensions, and certainly at least want to implement a function, not a few global variables to write an extension of it (fog
Here PHP provides us with a useful macro php_function. There are two functions defined in the generated code, which can be used to refer to its usage. The macro will eventually be translated into a function. For example Php_function (name) will eventually be translated into Voidzif_name (Zend_execute_data*execute_data,zval*return_value)
At the same time we see that there is a definition of such an array
Constzend_function_entry Cesium_functions[]={php_fe (Cesium_test1,arginfo_cesium_test1) PHP_FE (Cesium_test2, ARGINFO_CESIUM_TEST2) Php_fe_end};constzend_function_entry Cesium_functions[]={php_fe (cesium_test1,arginfo_ Cesium_test1) Php_fe (cesium_test2,arginfo_cesium_test2) Php_fe (name,null) Php_fe_end};
Remember, do not add semicolons or commas at the end. Finally, we can have one output of this function
Php_function (name) {php_printf ("Hellon");}
We can use this function after the compilation is finished.