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

How to implement hello word in PHP7 extension development

小云云
Release: 2023-03-19 14:38:01
Original
1834 people have browsed it

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();
?>
Copy after login

Output content:


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

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
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])
Copy after login

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])
Copy after login

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

Find

PHP_FE(confirm_say_compiled, in Add the following code above:


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

Four steps, 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 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!

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