PHP 7 extension development tutorial-Hello World implementation example, php7hello

Source: Internet
Author: User

PHP 7 extension development tutorial-Hello World implementation example, php7hello

The example in this article describes how to implement the PHP 7 extension development tutorial "Hello World. We will share this with you for your reference. The details are as follows:

1. Download PHP source code

To develop PHP extensions, You need to download the PHP source code first. On the one hand, our extensions generally use PHP-defined functions and macros, and on the other hand, we can use official tools to reduce the workload.

I downloaded the PHP-7.0.2 at http://cn2.php.net/get/php-7.0.2.tar.gz.

Decompress the source code compressed package, tar xzf php-7.0.2.tar.gz, we now only need to pay attention to the Zend and ext these two directories.

The Zend directory contains the PHP Zend Engine Source Code. For some functions and macro definitions, we need to take a brief look here.

The ext directory contains PHP native extensions and the tools we can use to develop our own extensions. ext_skel is used in Linux and ext_skel_win32.php is used in Windows.

Ii. Use the ext_skel Tool

We can see all PHP native extensions in the ext directory, including the familiar curl, json, mbstring, simplexml, and sockets extensions, there are also many extensions that have not been used or even never heard of. Don't worry about them. Let's first open the curl we are most familiar with to see, there are config. the m4 configuration file contains Source Code such as php_curl.h, curl_file.c, some intermediate files, and a tests Directory, which contains the curl extension unit test. Focus on config. m4, php_curl.h, and curl_file.c. In the simplest scenario, these three files are all components of an extension.

It is not too complicated to open it. However, it is a headache to write a similar one. In this case, you need to use the ext_skel tool I mentioned earlier. This tool is also in the ext directory. We can execute./ext_skel -- help to see several parameters. We only use -- extname = module, and enter the extension name developed by ourselves here. To learn more about the role of each parameter here: http://php.net/manual/en/internals2.buildsys.skeleton.php

./ext_skel --extname=hello

The ext directory contains a hello Directory, which will be used for subsequent work. The tool has automatically generated some files for us.

Config. m4 configuration file

Develop a PHP extension. before writing C code, configure it here. We can see the detailed description of the annotation, dnl is the annotation syntax.

If your extension uses external dependencies, configure the -- with-hello option. Otherwise, configure the -- enable-hello option to delete the del comments of the following three rows.

PHP_ARG_ENABLE(hello, whether to enable hello support,Make sure that the comment is aligned:[ --enable-hello      Enable hello support])

The PHP_ARG_WITH and PHP_ARG_ENABLE macros are used to configure the configure options. One configuration requires external dependencies, and the other configuration does not require external dependencies.

The configured content will be executed laterconfigure --helpYou can see.

Php_hello.h header file

A header file similar to C voice contains some custom structures and function declarations. This demo does not need to be modified for the time being.

Hello. c code file

The real logic code is in this file, which will be detailed later.

3. write code

Now, we have to start writing code to open the hello. c file.

The endpoint of the entire extension is the zend_module_entry structure. The specific definition can be found in the zend_modules.h file under the Zend directory. There are a total of more than a dozen attributes, which can be skipped quickly, for now, we only need "hello world ".

zend_module_entry hello_module_entry = {  STANDARD_MODULE_HEADER,  "hello",  hello_functions,  PHP_MINIT(hello),  PHP_MSHUTDOWN(hello),  PHP_RINIT(hello),    /* Replace with NULL if there's nothing to do at request start */  PHP_RSHUTDOWN(hello),  /* Replace with NULL if there's nothing to do at request end */  PHP_MINFO(hello),  PHP_HELLO_VERSION,  STANDARD_MODULE_PROPERTIES};

STANDARD_MODULE_HEADER helps us implement the first six attributes

"Hello" is the extension name

Hello_functions is a set of all methods included in the extension.

The next five macros represent five specific extension methods.

PHP_HELLO_VERSION is the extension version, which is defined in the header file.

STANDARD_MODULE_PROPERTIES helps us implement the remaining attributes

It does not need to be modified for the time being. You only need to know that this is an entry. Next to this entry, let's continue to see how to add a method to the extension. In the hello_functions [] method array, we already have an example method confirm_hello_compiled. Let's refer to it to write our method hello_world.

const zend_function_entry hello_functions[] = {  PHP_FE(confirm_hello_compiled, NULL)    /* For testing, remove later. */  PHP_FE(hello_world, NULL)  PHP_FE_END /* Must be the last line in hello_functions[] */};

Add hello_world to the extended method array, and then define hello_world. Find the definition of the confirm_hello_compiled method, and follow the instructions below. php_printf is the printf method in Zend Engine.

PHP_FUNCTION(hello_world){  php_printf("Hello World!\n");  RETURN_TRUE;}

Iv. Compilation and Installation

The last step is to compile and install our extensions. If you have installed PHP extensions, you don't need to check them. If you have no experience, you can refer to them.

Phpize

./configuremakemake install

Now the PHP extension directory contains the hello. so file. Add the extension configuration in php. ini.

extension = hello.so

V. Test

Write a test. php method and execute the script to see "Hello World! "

<?phphello_world();

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.