Helloword for PHP 7 extension development

Source: Internet
Author: User
Helloword for PHP 7 extension development this article uses PHP7 as the basis to explain how to create a PHP extension from scratch. This document describes the basic steps for creating an extension. In this example, we will implement the following functions:

 

Output Content:

$ php ./test.php$ hello word

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

Step 1: generate code

PHP provides an ext_skel tool for generating basic code. 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 will be generated under the current directory.

Step 2: modify the config. m4 configuration file

Config. m4 works with the phpize tool to generate the configure file. The configure file is used for environment detection. Check whether the environment required for extended compilation and running meets the requirements. Now let's modify the config. m4 file.

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

Open the config. m4 file and you will find such a text.

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

Dnl is the annotator. The above code says that if the extension you write depends on other extensions or lib libraries, you need to remove the comments of the PHP_ARG_WITH code. Otherwise, remove the comments of the PHP_ARG_ENABLE-related code segments. The extensions we write do not need to depend on other extensions and lib libraries. Therefore, we remove the comments before PHP_ARG_ENABLE. The code after removing the annotation 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 on it:

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

Find PHP_FE (confirm_say_compiled, and 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[] */ }; /* }}} */
Step 4: compile and install

Follow these steps to compile the extension:

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

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

[say]extension = say.so

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

Step 5: Call the test

Write a script and call the say method. Check whether the output content meets expectations.

Download complete code

Complete download of extended source code.

Link to the original article: hello word for PHP 7 extension development. For more information, see source!

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.