PHP extension development tutorial _ php instance

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 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 following 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 */

The main code here is to define a macro named phpext_myext2_ptr. The PHP underlying layer uses this macro to reference our extensions. We can see that the macro name is also standardized-phpext _ extension _ ptr. Myext2_module_entry is the struct we will define later in the. c file. Its name is also standard-extension _ module_entry.

In addition, we also define a macro that identifies our extended version number and a function prototype (through the PHP_FUNCTION macro, The PHP_FUNCTION macro parameter is an externally usable function name ), we will implement this function later.

Finally, let's look at the implementation of the myext2.c file:

The Code is as follows:


# Include "php. h"
# Include "php_myext2.h"

/* {Myext2_functions []
*
* Every user visible function must have an entry in myext2_functions [].
*/
Static const zend_function_entry myext2_functions [] = {
PHP_FE (hello, NULL)
PHP_FE_END
};
/*}}}*/

/* {Myext2_module_entry
*/
Zend_module_entry myext2_module_entry = {
STANDARD_MODULE_HEADER,
"Myext2",/* module name */
Myext2_functions,/* module functions */
NULL,/* module initialize */
NULL,/* module shutdown */
NULL,/* request initialize */
NULL,/* request shutdown */
NULL,/* phpinfo */
PHP_MYEXT2_VERSION,/* module version */
STANDARD_MODULE_PROPERTIES
};
/*}}}*/

# Ifdef COMPILE_DL_MYEXT2
ZEND_GET_MODULE (myext2)
# Endif

/* {Proto void hello ()
Print "hello world! "*/
PHP_FUNCTION (hello)
{
Php_printf ("hello world! \ N ");
}
/*}}}*/

Compared with the. c file created by the extension skeleton, we can find that our. c file is very simple. In fact, these are enough for a basic extension.

The above code is simple and clear, and most comments are descriptive. Let's briefly summarize:

1. Start with the header file that we will use. Php. h is required. It has helped us to include the vast majority of standard library files we will use, such as stdio. h and stdlib. h.
2. myext2_functions defines the struct array composed of the functions to be exposed. Each element is specified through the PHP_FE macro. The PHP_FE macro has two parameters: the first is the function name that can be used externally, the second is the parameter information (NULL is used here), and the last element must be PHP_FE_END. Note its comments and emphasize that every function to be exposed to external use must be defined in the struct array.
3. myext2_module_entry defines our module information. It is a struct, and most of the attributes have been commented out. Pay attention to the five function pointers in the middle. We simply set them to NULL and will describe their usage in subsequent blog posts.
4. The ZEND_GET_MODULE (myext2) Macro function is included by the ifdef macro, so whether it is called depends on the situation. As to the circumstances under which the API is called and under which circumstances it will not be called, it will be described in subsequent blog posts.
5. We implemented the hello function in the last few lines of code. It is very simple to call php_printf to output hello world! With a line break, php_printf is used exactly the same as printf.
6. The {And} in the comments are used to facilitate editor folding such as vim. We recommend that you write comments as well.
Some macros are involved here, such as PHP_FE, PHP_FE_END, and PHP_FUNCTION. the complete introduction of these macros will only be available in subsequent blog posts. The simplest way is to remember these macros.

Note that the naming of each file, the naming of variables, spaces, indentation, and comments are very standard and follow these specifications, we recommend that you use this specification to develop PHP extensions.

Finally, compile and run our extension:

The Code is as follows:


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

$ Php-dev-m | grep myext2
Myext2
$ Php-dev-r 'Hello ();'
Hello world!

Related Article

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.