Teach you step by step to create a php extension (basic steps)

不言
Release: 2023-04-03 22:30:02
Original
1740 people have browsed it

The content of this article is about teaching you step by step to create a php extension (basic steps). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What are the basic steps to create an extension. In the example, we will implement the following functions:

<?php
echo say();
?>
Copy after login

Output content:

$ php ./test.php
$ hello word
Copy after login

Implement a say method in the extension, and after calling the say method, output the hello word.

Step one: Generate code

PHP provides us with the tool ext_skel to generate basic code. This tool is in the ./ext directory of the PHP source code.

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

The value of the extname parameter is the extension name. After executing the ext_skel command, a directory with the same extension name will be generated in the current directory.

The second step is to modify the config.m4 configuration file

The function of config.m4 is to cooperate with the phpize tool to generate the configure file. The configure file is used for environment detection. Check whether the environment required for extension compilation and running is met. Now we start to modify the config.m4 file.

$ cd ./say
$ vim ./config.m4
Copy after login

After opening the config.m4 file, you will find this paragraph.

dnl If your extension references something external, use with:

dnl PHP_ARG_WITH(say, for say support,
dnl Make sure that 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 that the comment is aligned:
dnl [ --enable-say                 Enable say support])

Among them, dnl is the comment symbol. The above code says that if the extension you write depends on other extensions or lib libraries, you need to remove the comments on the PHP_ARG_WITH related code. Otherwise, uncomment the PHP_ARG_ENABLE related code section. The extensions we write do not need to rely on other extensions and lib libraries. Therefore, we remove the comment in front of PHP_ARG_ENABLE. The code after removing the comments is as follows:

dnl If your extension references something external, use with:

dnl PHP_ARG_WITH(say, for say support,
dnl Make sure that the comment is aligned ; comment is aligned:
[ --enable-say                 Enable say support])

The third step is to implement the code

Modify the say.c file. Implement the say method.
Find PHP_FUNCTION(confirm_say_compiled), add the following code above it:

PHP_FUNCTION(say)
{
    zend_string *strg;
    strg = strpprintf(0, "hello word");
    RETURN_STR(strg);
}
Copy after login

Find PHP_FE(confirm_say_compiled), add the following code above it:

PHP_FE(say, NULL)
Copy after login

The modified code is as follows:

 const zend_function_entry say_functions[] = {
 PHP_FE(say, NULL)       /* For testing, remove later. */
 PHP_FE(confirm_say_compiled,    NULL)       /* For testing, remove later. */
 PHP_FE_END  /* Must be the last line in say_functions[] */
 };
/ }}} /
Copy after login

The fourth step, compile and install

The steps to compile the extension are as follows:

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

Modify the php.ini file and add the following code:

[say]
extension = say.so
Copy after login

Then execute, php -m command. In the output content, you will see the word say.

The fifth step is to call the test

Write a script yourself and call the say method. See if the output content conforms to Expected.

Related recommendations:

[Transfer] Write your own PHP extension to create a class - orlion

Create one from scratch PHP extension

The above is the detailed content of Teach you step by step to create a php extension (basic steps). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!