Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write PHP extension

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to write PHP extension". In daily operation, I believe many people have doubts about how to write PHP extension. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to write PHP extension". Next, please follow the editor to study!

Start with hello world (trial reading)

Based on PHP7, the following shows how to create a PHP extension from scratch. In the example, we will implement the following functions:

Output:

$php. / test.php $hello word

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

* step: generate code

PHP provides us with a tool for generating basic code, ext_skel. The tool is in the. / ext directory of the PHP source code.

$cd php_src/ext/ $. / ext_skel-- extname=say

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

Step 2: modify the config.m4 configuration file

The role of config.m4 is to cooperate with phpize tools to generate configure files. The configure file is used for environmental detection. Check whether the environment required for the extension compilation to run is satisfied. Now let's modify the config.m4 file.

$cd. / say $vim. / config.m4

When you open the config.m4 file, you will find a paragraph like this.

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])

Where dnl is the annotation symbol. The above code says that if you write an extension that relies on other extensions or lib libraries, you need to uncomment the PHP_ARG_WITH-related code. Otherwise, the comments related to the PHP_ARG_ENABLE code snippet are removed. The extensions we write do not need to rely on other extensions and lib libraries.

Therefore, we remove the comments in front of PHP_ARG_ENABLE. The code with the comments removed 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: dnl [--with-say Include say support]) dnl Otherwise use enable: PHP_ARG_ENABLE (say, whether to enable say support, Make sure that the comment is aligned: [--enable-say Enable say support]) step 3: code implementation

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

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

Find PHP_FE (confirm_say_compiled) and add the following code to it:

PHP_FE (say, NULL)

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 [] * /}; / *}} * / step 4: compile and install

The steps to compile the extension are as follows:

$phpize $. / configure $make & & make install

Modify the php.ini file by adding the following code:

[say] extension = say.so

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

Step 5: invoke the test

Write your own script and call the say method. See if the output meets expectations.

Output:

$php. / test.php $hello word at this point, the study on "how to write PHP extensions" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report