PHP extension development (1): Getting Started, php extension _ PHP Tutorial

Source: Internet
Author: User
Tags sapi zts
PHP extension development (1): Getting Started, php extension. PHP extension development (1): Getting Started. php extensions have many articles and blogs about PHP extension development. The typical ones are: I am going to summarize my PHP extension development in this series of blog posts (1): Getting Started, PHP extension

There are already many articles and blogs about PHP extension development, which are classic ones:

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:

$ ./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:

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

Some command line options are useful:

Php-dev-h # Print Help information php-dev-v # Print version information php-dev -- ini # print configuration information php-dev-m # print loaded module information php- dev-I # phpinfophp-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:

$ ./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:

$ ./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:

$ php-dev -m | grep myextmyext$ 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:

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:

 1 #ifndef PHP_MYEXT2_H 2 #define PHP_MYEXT2_H 3  4 extern zend_module_entry myext2_module_entry; 5 #define phpext_myext2_ptr &myext2_module_entry 6  7 #define PHP_MYEXT2_VERSION "0.1.0" 8  9 /* prototypes */10 PHP_FUNCTION(hello);11 12 #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:

 1 #include "php.h" 2 #include "php_myext2.h" 3  4 /* {{{ myext2_functions[] 5  * 6  * Every user visible function must have an entry in myext2_functions[]. 7  */ 8 static const zend_function_entry myext2_functions[] = { 9     PHP_FE(hello,       NULL)10     PHP_FE_END11 };12 /* }}} */13 14 /* {{{ myext2_module_entry15  */16 zend_module_entry myext2_module_entry = {17     STANDARD_MODULE_HEADER,18     "myext2",               /* module name */19     myext2_functions,       /* module functions */20     NULL,                   /* module initialize */21     NULL,                   /* module shutdown */22     NULL,                   /* request initialize */23     NULL,                   /* request shutdown */24     NULL,                   /* phpinfo */25     PHP_MYEXT2_VERSION,     /* module version */26     STANDARD_MODULE_PROPERTIES27 };28 /* }}} */29 30 #ifdef COMPILE_DL_MYEXT231 ZEND_GET_MODULE(myext2)32 #endif33 34 /* {{{ proto void hello()35    Print "hello world!" */36 PHP_FUNCTION(hello)37 {38     php_printf("hello world!\n");39 }40 /* }}} */

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:

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:

$ ./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 myext2myext2$ php-dev -r 'hello();'hello world!

Tips (1): Getting Started. php extensions have a lot of articles and blogs on PHP extension development. the classic ones are: I am going to summarize my PHP extension development in this series of blog posts...

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.