Detailed Explanation of hello word Implementation Method for PHP7 Extension Development

  • 2021-09-04 23:37:23
  • OfStack

This article illustrates the implementation of hello word for PHP7 extension development. Share it for your reference, as follows:

Here is an explanation of how to create an PHP extension from scratch, based on PHP 7. This article mainly explains the basic steps of creating an extension. In the example, we will implement the following functions:


<?php
echo say();
?>

Output:


$ php ./test.php
$ hello word

One say method is implemented in the extension. After calling say method, hello word is output.

Step 1: Generate code

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


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

The value of the extname parameter is the extension name. When the command ext_skel is executed, a directory with the extension 1 will be generated under the current directory.

Step 2: Modify the config. m4 configuration file

config. m4 works with the phpize tool to generate configure files. The configure file is used for environment detection. Check whether the environment required for the extension compilation to run is satisfied. Now we begin to 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 a comment symbol. The above code says that if you write an extension that relies on other extensions or the lib library, you need to remove the comments on the relevant code of PHP_ARG_WITH. Otherwise, remove the comment for the relevant code snippet of PHP_ARG_ENABLE. The extensions we write do not need to rely on other extensions and lib libraries. Therefore, we remove the comment before PHP_ARG_ENABLE. The code without 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:
 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) 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 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 to add the following code:


$ php ./test.php
$ hello word

0

And then execute, php -m Orders. In the output, you will see the words say.

Step 5, invoke the test

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

For more readers interested in PHP related content, please check the topics on this site: "PHP Extension Development Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php+mysql Database Operation Introduction Tutorial", "PHP Network Programming Skills Summary" and "php Common Database Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: