Home >Backend Development >PHP Tutorial >How to implement hello word in PHP7 extension development

How to implement hello word in PHP7 extension development

小云云
小云云Original
2018-01-16 13:45:331827browse

This article mainly introduces the implementation method of hello word in PHP7 extension development. It analyzes the specific steps and related operating techniques of PHP7 extension development in the form of examples. It involves the modification and compilation of the underlying source code of PHP. Friends who need it can refer to it. I hope Can help everyone.

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


<?php
echo say();
?>

Output content:


$ php ./test.php
$ hello word

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

Step one: Generate code

PHP provides us with a tool to generate basic code ext_skel. This 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 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

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, remove the comments from the PHP_ARG_ENABLE related code segment. 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:
 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])

The third step is to implement the code

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


##

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

Find

PHP_FE(confirm_say_compiled, in Add the following code above:


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[] */
 };
 /* }}} */

Four steps, compile and install

The steps to compile the extension are as follows:


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

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

##

[say]
extension = say.so

Then execute the

php -m

command. In the output, you will see the word ##.

#The fifth step is to call the test

##Write a script yourself and call the say method to see if the output content is as expected. Related recommendations:

Introduction to new features in PHP7

PHP7 error handling and exception handling methods

Building a performance testing environment for PHP7 method

The above is the detailed content of How to implement hello word in PHP7 extension development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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