Home >Backend Development >PHP Tutorial >Step-by-step guide to writing PHP extensions
1. Write it firstWith the rapid development of the Internet and the popularity of lamp architecture, PHP supports more and more extensions, which directly promotes the development of PHP. However, PHP also has inevitable problems with scripting languages. Its performance is much different than compiled languages such as C, so when considering performance issues, it is best to solve them through PHP extensions. So, how to make a php extension. Let's start with an example (this article requires C basics). 2. Solve a problemIn a system, if the sum of squares of an array is often required, we can write it like this.
During actual execution, the php zend engine will translate this paragraph into C language, and memory allocation is required every time. So the performance is relatively poor. Furthermore, based on performance considerations, we can write an extension to do this. 3. Write extensionsTo build an extension, at least 2 files are required. One is the Configulator file, which tells the compiler which at least dependent libraries are required to compile this extension; the second is the actual execution file. 3.1 Generate frameworkIt sounds complicated, but there is actually a tool that can help us figure out an extended framework. There is a tool ext_skel in the PHP source code, which can help us generate an extension framework.
Now we use it to generate extended array_square_sum. ($ represents prompt command)
After executing the command, the terminal tells us how to produce a new extension. Check the file content and you will find that there are several more important files: config.m4, php_array_square_sum.h, array_square_sum.c, which will be described one by one below.
3.2 Configuration file config.m4
Remove dnl
This is the minimum requirement to be able to call the enable-sample option when ./configure. The second parameter of PHP_ARG_ENABLE will be displayed when this extended configuration file is reached during ./configure processing. The third parameter will be executed by the end user. /configure --help is displayed as help information 3.3 Header fileModify php_array_square_sum.h and change confirm_array_square_sum_compiled to confirm_array_square_sum. This is the name of the function we will actually call in the future. Of course, you can also directly add the function confirm_array_square_sum without deleting confirm_array_square_sum_compiled.
It should be
3.3 Source codeModify array_square_sum.c and change confirm_array_square_sum_compiled to confirm_array_square_sum. This is the function to register this extension. If confirm_array_square_sum was directly added in 3.2, just add confirm_array_square_sum directly in this step.
and change it to
Then the most critical step is to rewrite confirm_array_square_sum. At this time, you only need to rewrite confirm_array_square_sum_compiled into confirm_array_square_sum (confirm_array_square_sum_compiled was not deleted in 3.1, so you need to add confirm_array_square_sum).
Rewritten as
PHP is a weakly typed language, and its data is stored in the structure zval ( Please see more professional information for details, such as "php extension development.pdf").
In order to obtain the parameters passed by the function, you can use the zend_parse_parameters() API function. The following is the prototype of the function:
We can directly generate the first few parameters of the zend_parse_parameters() function using macros in the kernel, in the form: ZEND_NUM_ARGS() TSRMLS_CC. Note that there is a space between the two, but there is no comma. As can be seen from the name, ZEND_NUM_ARGS() represents the number of parameters. Followed by common parameter types (similar to printf in C language), followed by common parameter lists.
The development of this main function is finally completed. 3.4 Generate configure file Then execute ~/php/bin/phpize
You can find that the executable script configure appears in array_square_sum. 3.5 CompilationIt is best to bring php-config PATH when compiling, because the system default php-config-path may not be the php path you are currently using.
is compiled successfully, the terminal will have the following prompt:
Check the module directory of the array_square_sum directory, and you will find that array_square_sum.so is generated in it. This is the extension we need.
4. Use extensions4.1. Configuration extensionModify the php configuration php.ini and add the configuration content.
4.2. Add modulePHP extensions are generally in $PHP_PATH/lib/php/extensions/no-debug-non-zts-yyyymmdd. If you can’t find it, please go to Baidu or Google. There are many .so files in it. Just copy array_sum_square.so produced in 3.5. If you use fastcgi mode, you need to restart php, so our php should have the extension array_square_sum, You can check phpinfo for details (please use Baidu or Google if you don’t know). 4.2. Write codeSince writing extensions can improve operating efficiency, here we compare and test performance by using extensions and directly using PHP code. Experimenting multiple times can reduce errors, so do 2,000 times to find the sum of the squares of 100,000 numbers. The code is as follows:
The test results are as follows:
You can see that the extension is 15 times faster than using php directly. As business logic becomes more complex, this differentiation will become greater. Then use c language to do this directly. Here is a code to test (the test conditions are exactly the same):
Execute and view the effect. It can be seen that the time of using C directly is only 0.261746, which is 13.09% of using C extension and 0.87% of using PHP directly. Of course, if complex operations such as IO are involved, C/C++ will be tens of thousands of times faster than PHP (tested).
Therefore, in actual services that require very high performance requirements, such as indexing, word segmentation, etc., you can use C to create a set of underlying services and use PHP to encapsulate the calls. |