Home > Backend Development > PHP Tutorial > PHP extension basic learning

PHP extension basic learning

小云云
Release: 2023-03-19 16:24:01
Original
1560 people have browsed it

This article mainly introduces you to writing PHP extensions from scratch. We will start with how to generate a PHP extension. I hope it can help you.

PHP is written in C language. For every PHPer, there is an inner urge to write extensions. However, a good entry point is lacking. When searching PHP extension development on Google, most of them are copied articles, and some people even posted them on their blogs without even operating them. However, there are several good tutorials, but they are all products of the PHP 5 era and have many hidden pitfalls. I will record the process of slowly stepping on the pit, and maybe this will become a "tutorial" for others.

Generate an extension

I believe many people have seen many online tutorials. Most of them teach us to execute this command: $./ext_skel--extname=extname. However, when you clone the PHP source code, you will find that there is no ext/ext_skel file under the master branch. So, I summed it up:

If you download the source code of PHP directly, or under the released version, you can execute this command

$ cd ext
$ ./ext_skel --extname=extname
Copy after login

If you directly download it from the master Under the branch, there is only the ext_skel.php file. At this time, you can directly execute this PHP file

$ cd ext
$ php ext_skel.php --ext extname
Copy after login

Since I developed it directly under the master branch, the following operations are all operations under the master branch by default. .

After generating the extension, we will see four files and a folder. At this stage, we only need to use two files, the .c file and the .h file.

A small pit

After we generate the extension, we can try to compile it

$ phpize
$ ./configure
$ make &&make test
Copy after login

We will be surprised to find that there will be a warning during compilation.

warning:implicitdeclaration of function
'ZEND_PARSE_PARAMETERS_NONE'isinvalid inC99 [-Wimplicit-function-declaration]
ZEND_PARSE_PARAMETERS_NONE();
^
Copy after login

1warning generated.

Then you execute make test and find that one test failed. That's right, the files generated by the script for us actually failed our own tests. Do you think it's weird? Let's look at the specific information of warning. Function ZEND_PARSE_PARAMETERS_NONE not found. I looked at the file and found it on line 15. You can probably guess what it means by looking at the function name. So I went to search the PHP source code. But we found such a macro definition.

#ifndefzend_parse_parameters_none
#definezend_parse_parameters_none()
zend_parse_parameters(ZEND_NUM_ARGS(),"")
#endif
Copy after login

After replacing the original capitalization, there will be no warning. This can be regarded as the official digging a small hole for us. Although there are macro definitions in capital letters, I am not sure why an error is reported.

Define a function

I think that when most people write extensions, they definitely hope to implement at least one function. They don’t just need a few global variables to write an extension (fog

Here PHP provides us with a useful macro PHP_FUNCTION. There are also two functions defined in the generated code. You can refer to its usage. For example, PHP_FUNCTION(name). It will eventually be translated into voidzif_name(zend_execute_data*execute_data,zval*return_value)

At the same time, we see that such an array is defined

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
};
Copy after login

Remember, do not add a semicolon or comma at the end. , we can output this function

PHP_FUNCTION(name)
{
php_printf("Hellon");
}
Copy after login

After compilation and installation, we can use this function

Related recommendations:

PHP extension Tencent CMEM. Compilation

Automatic generation of extension framework for php extension

How to write a php extension

The above is the detailed content of PHP extension basic learning. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template