PHP extension development tutorial

Source: Internet
Author: User
Tags sapi zts
This article mainly introduces the PHP extension development tutorial. This article describes the basic knowledge of using C language to develop a PHP extension in a Linux system. For more information, see

This article mainly introduces the PHP extension development tutorial. This article describes the basic knowledge of using C language to develop a PHP extension in a Linux system. For more information, see

PHP extension development

I am going to summarize my learning and insights on PHP extension development in this series of blog posts, and try to briefly and clearly describe the most basic knowledge that a PHP extension should possess in a Linux system. The level is low, and errors are inevitable.

Preparations

Obtain the PHP source code (you can check it out from Github or download the latest stable version on the official website) and compile it. To speed up compilation, we recommend disabling all additional extensions (using the -- disable-all option), but it is best to enable debug (using the -- enable-debug option) and thread security (use -- enable-maintainer-zts), but Disable debug when releasing the extension, depending on the situation, choose whether to enable thread security:

The Code is as follows:


$./Buildconf -- force
$./Configure -- disable-all -- enable-debug -- enable-maintainer-zts
$ Make


Note that the -- prefix option is not specified (and the make install option is not required. Check the output information. You may need to install some dependency packages to compile PHP.

The compiled PHP executable program is located in the sapi directory of the source code and has different subdirectories for different host environments. We will mainly use the cli (command line interface) environment in the future, you can create an alias to facilitate reference:

The Code is as follows:


$ Alias php-dev =/usr/local/src/php-5.6.5/sapi/cli/php

Some command line options are useful:

The Code is as follows:


Php-dev-h # print help information
Php-dev-v # print version information
Php-dev -- ini # print configuration information
Php-dev-m # print the loaded module information
Php-dev-I # phpinfo
Php-dev-r# Execute the code in the code

Extension skeleton

All official extensions of PHP are under the ext directory of the source code, and the extensions we write can also be placed under this directory. Note that there is a shell script named ext_skel under this Directory, which is used to generate the PHP extension skeleton. Using this script can help us quickly create PHP extensions:

The Code is as follows:


$ ./ext_skel --extname=myext


The above command helped us create an extension named myext. The source code is under the myext directory. Run the script without any parameters to print the help information, so that you can view more options provided by the script.

Next let's complete our expansion. Go to the myext directory, edit the config. m4 configuration file, find the PHP_ARG_ENABLE macro function, and remove the preceding dnl annotation (three lines in total ). Return to the source code root directory and run the buildconf, configure, and make commands again:

The Code is as follows:


$./Buildconf -- force
$./Configure -- help | grep myext
-- Enable-myext Enable myext support
$./Configure -- disable-all -- enable-myext -- enable-debug -- enable-maintainer-zts
$ Make

Note: we use. /configure -- help | grep myext prints the loading of our extensions. If the following output is not displayed, it indicates that our extension has not been configured successfully. Check the config later. m4 file.

This compilation should be very fast, because most of the Code has been compiled. PHP also has another method for compiling extensions (using dynamic connections to compile extensions. so files), but we recommend that you use static compilation during development extension, because this saves the step of loading the extension in the configuration file.

If everything goes well, our first extension can be executed:

The Code is as follows:


$ Php-dev-m | grep myext
Myext
$ Php-dev-r 'echo confirm_myext_compiled ("myext"). "\ n ";'
Congratulations! You have successfully modified ext/myext/config. m4. Module myext is now compiled into PHP.


The first command shows that our extension has been loaded. The second command executes the ext_skel extension skeleton to automatically create a function for us. Of course, this function is meaningless, but we can easily adapt it to hello world.

Manually create Extension

Most of the tutorials describe extension development based on the ext_skel extension skeleton, which is convenient and convenient. But I personally prefer the method of manual development extension, because it is easier to understand every detail.

Manually create an extension. First, go to the ext directory and create our extension directory myext2. Several files are required: config. m4, myext2.c, and php_myext2.h.

First, write the configuration file config. m4:

The Code is as follows:


PHP_ARG_ENABLE (myext2, whether to enable myext2 support,
[-- Enable-myext2 Enable myext2 support])

If test "PHP_MYEXT2 "! = "No"; then
PHP_NEW_EXTENSION (myext2, myext2.c, $ ext_shared)
Fi


Config. m4 is actually the configuration file used by the autoconf program. autoconf is an important component in the autotools toolbox. It takes a long time to fully introduce the usage of autoconf. Fortunately, our usage here is very simple.

PHP_ARG_ENABLE is a macro function defined by PHP for autoconf. myext2 is its first parameter, indicating the extension name. The latter two parameters are only used for display during make and configure execution, so we can write it at will. [] In autoconf syntax, the function is similar to double quotation marks, used to enclose strings (note that the second parameter contains spaces, but it can be enclosed in square brackets ). The fourth parameter is used to specify whether the extension is enabled or disabled by default (yes or no). The default parameter is no.

The following three lines are actually shell syntax, which determines whether the PHP_MYEXT2 extension module is enabled. If the extension module (-- enable-myext2) is enabled, the value of the $ PHP_MYEXT2 variable is not no, so execute the PHP_NEW_EXTENSION macro. This macro function is also the extension syntax defined by PHP for autoconf. The first parameter is the extension name, and the second parameter is the extension of the C file to be compiled. If there are multiple, write it down in sequence (separated by spaces). The third parameter is fixed to $ ext_shared.

Next, compile the php_myext2.h header file named PHP extension specification-php _ extension. h:

The Code is as follows:


# Ifndef PHP_MYEXT2_H
# Define PHP_MYEXT2_H

Extern zend_module_entry myext2_module_entry;
# Define phpext_myext2_ptr & myext2_module_entry

# Define PHP_MYEXT2_VERSION "0.1.0"

/* Prototypes */
PHP_FUNCTION (hello );

# Endif/* PHP_MYEXT2_H */

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.