PHP7 extension development of the Hello Word implementation method

Source: Internet
Author: User
Tags php source code
This article mainly introduced the PHP7 extension development of the Hello Word implementation method, combined with the example form analysis of the PHP7 expansion development of the specific steps and related operation skills, involving the PHP underlying source code modification and compilation, the need for friends can refer to, hope to help everyone.

This is based on PHP7, which explains how to create a php extension from scratch. This article explains what the basic steps of creating an extension are. example, we will implement the following functions:


<?phpecho say ();? >

Output content:


$ php./test.php$ Hello Word

Implement a say method in the extension, and after calling the say method, output Hello Word.

First step: Generate code

PHP provides us with the tool Ext_skelto generate the codebase. This tool is in the PHP source code under the./ext directory.


$ 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 will be generated under the current directory.

Step two, modify the CONFIG.M4 configuration file

The role of CONFIG.M4 is to generate configure files with the Phpize tool. The Configure file is used for environmental testing. Detects if the environment required for the extended compilation run is satisfied. Now we begin to modify the Config.m4 file.


$ CD./say$ vim./config.m4

After opening, CONFIG.M4 file, you will find such a paragraph of text.


DNL If your extension references something external, use WITH:DNL php_arg_with (say, for say SUPPORT,DNL do 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, the comment is ALIGNED:DNL [--enable-say      enable say support])

Where dnl is an annotation symbol. The code above says that if you write an extension that relies on other extensions or LIB libraries, you need to remove the comments from the Php_arg_with related code. Otherwise, remove the comment for the php_arg_enable related code snippet. The extensions we write do not need to rely on other extensions and LIB libraries. Therefore, we remove the comments in front of the php_arg_enable . The code after removing the comment is as follows:


DNL If your extension references something external, use WITH:DNL php_arg_with (say, for say support, dnl make sure that T  He comment is aligned:dnl [--with-say       Include say support]) DNL Otherwise use enable:php_arg_enable (say, whether to Enable say support, make sure, the comment are aligned: [--enable-say      enable say support])

The third step, the code implementation

Modify the Say.c file. Implement the Say method.
Find PHP_FUNCTION(confirm_say_compiled) , add the following code on top of 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 the 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_functi ons[] */}; /* }}} */

Fourth step, 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:


[say]extension = say.so

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

Fifth step, call the test

Write a script yourself, calling the Say method. See if the output matches the expected content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.