Introduction to PHP Extension Development tutorial _php instance

Source: Internet
Author: User
Tags comments function prototype php source code phpinfo sapi zts

PHP Expansion Development

I'm going to summarize my learning and insights about PHP expansion development in this series, and try to describe the basics of developing a PHP extension in a simple and clear way with a Linux system. The level is low, inevitably has the mistake, hoped pointed out.

Preparatory work

First get a copy of PHP source code (can be checked out from the GitHub, or to the official web download the latest stable version), and then compile it. To speed up compilation, we recommend disabling all additional extensions (using the--disable-all option), but it is best to turn on debug (using the--enable-debug option) and thread safety (using--ENABLE-MAINTAINER-ZTS). However, to turn off debug when the extension is published, choose whether to turn on thread safety as appropriate:

Copy Code code as follows:

$./buildconf--force
$./configure--disable-all--enable-debug--enable-maintainer-zts
$ make

Note that we do not specify the--prefix option (and no make install), as this is not required. Note that the output information, you may need to install a number of dependent packages to successfully compile PHP.

Compiled PHP executable program in the source SAPI directory, corresponding to different hosting environment has different subdirectories, we will mainly use the CLI (Command line interface) environment, you can build an alias convenient reference:

Copy Code code as follows:

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

There are some command-line options that can be useful:

Copy Code code as follows:

PHP-DEV-H # Printing Help information
PHP-DEV-V # Print version information
Php-dev--ini # Print configuration information
PHP-DEV-M # Print Loaded module information
Php-dev-i # Phpinfo
Php-dev-r <code> # Execute code codes

Extended skeleton

All of PHP's official extensions are in the source code of the Ext directory, our own written extensions can also be placed in this directory. Note that the directory has a shell script called Ext_skel, which is used to build the PHP extension skeleton, which can help us quickly create PHP extensions:

Copy Code code as follows:

$./ext_skel--extname=myext

The above command helped us create an extension called Myext, the source code in the Myext directory. Execution with no parameters The script can print help information so that you can see more options available to the script.

Next let's finish our extension. Enter the Myext directory, edit the CONFIG.M4 configuration file, locate the Php_arg_enable macro function, and remove the previous DNL annotation (a total of three lines). Back to source root, re-execute buildconf, configure, and make commands:

Copy Code code 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 that we use./configure--help | grep Myext Prints our extended load, and if we don't see the output below, our extension is not configured successfully and we'll go back and check the Config.m4 file.

The compilation should be very fast because most of the code has already been compiled. PHP also has another way of compiling extensions (using dynamic connections to compile extensions as. So), but we recommend using static compilation when developing extensions, because this eliminates the step of loading extensions in a configuration file.

If everything goes well, our first extension is ready to go:

Copy Code code 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 performs the function that the Ext_skel extension skeleton automatically creates for us. Of course, this function is meaningless, but we can easily adapt this function to Hello World.

To create an extension manually

Most tutorials are based on the Ext_skel extension skeleton to describe the expansion of development, which is of course very convenient and quick. But I personally prefer to develop the extension purely by hand, because it makes it easier to understand each of these details.

Create extensions manually, first enter the EXT directory, and create our extended directory MYEXT2. There are several documents that are required: config.m4,myext2.c and php_myext2.h.

First, we'll write the configuration file config.m4:

Copy Code code 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 a configuration file used by the autoconf program, autoconf is an important component of the Autotools Toolbox. It will take a long time to fully introduce the usage of autoconf, but the usage here is very simple.

Php_arg_enable is the macro function defined by PHP for autoconf, MYEXT2 is its first argument, the name of the extension, and the following two parameters are only displayed when make and configure are executed, so we can write them casually. [] Functions in the autoconf syntax are similar to double quotes, which are used to wrap strings (note that the second argument contains spaces, but you can do without square brackets). There is also a fourth parameter that indicates whether the extension is open or closed by default (yes or no), and the default is No.

The next three lines are actually shell syntax, which determines whether we are opening the PHP_MYEXT2 extension module. If the extension module (--ENABLE-MYEXT2) is turned on, the value of the $PHP_MYEXT2 variable is not no, so the Php_new_extension macro is executed. This macro function is also the extension syntax defined by PHP for autoconf, the first parameter is also extended name, the second parameter is the extension to compile the C file, if there are more than one, then write down on it (space separated); The third parameter is fixed to $ext_shared.

Next, write the php_myext2.h header file, which is named after the PHP extension's specification-php_ extension. h:

Copy Code code 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 the definition of a macro named Phpext_myext2_ptr, which the bottom of PHP uses to refer to our extensions. As you can see, the name of the macro is also a canonical-phpext_ extension _ptr. And Myext2_module_entry is the struct that we will define later in the. c file, and its name is canonical-Extension _module_entry.

In addition, we have defined a macro that identifies our extended version number and a function prototype (by Php_function macros, the parameters of the Php_function macro function are the names of functions that can be used externally), and we will implement this function later.

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

Copy Code code as follows:

#include "Php.h"
#include "Php_myext2.h"

/* {{myext2_functions[]
*
* Every user visible function must have a 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");
}
/* }}} */

By contrast, the. c file created by the extended skeleton will find that our. c files are very simple, but they are sufficient for a basic extension.

The code above is simple and clear, and most annotations are already very descriptive. Let us briefly summarize the following:

1. Start with the header file we want to use. Php.h is a must, it has helped us to include most of the standard library files we use, such as stdio.h,stdlib.h and so on.
2.myext2_functions defines an array of structures that are composed of functions that we want to expose, each of which is specified by Php_fe macros. The PHP_FE macro has two parameters, the first is an externally available function name, and the second is parameter information (here we simply use NULL), and the last element must be php_fe_end. Note its annotations, emphasizing again that each function that is to be exposed to external use must be defined in the structure body array.
3.myext2_module_entry defines our module information, which is a struct, and most attributes have been explained by annotations. Notice the five function pointers in the middle, and we all simply set them to null, and in subsequent posting they will be described in their usage.
A 4.zend_get_module (MYEXT2) macro function is contained by a IFDEF macro, so whether it is invoked is dependent on the situation. As to what circumstances would be invoked and under what circumstances it would not be invoked, in subsequent posting would be narrated.
5. The last few lines of code we implemented the Hello function, it is very simple, call php_printf output Hello world! with a newline character, php_printf usage and printf exactly the same.
6. The {{{and}}} in the comments are used to facilitate the folding of the editor such as Vim, and we recommend that you write comments as well.
This involves a number of macros, such as Php_fe,php_fe_end,php_function, and so on, complete introduction of these macros to the subsequent posting, the simplest way is to remember these macros.

Note that each of our files is named, variable naming, spaces and indents, and annotations are very canonical, and following these specifications allows us to write code that is more compatible with PHP's own code, and we recommend that you use this specification to develop PHP extensions.

Finally, the compiler runs our extension:

Copy Code code 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!

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.