PHP extension development series in Linux: 2. a typical extension development

Source: Internet
Author: User
Tags prototype definition zts
PHP extension development series in Linux: 2. after reading some of the content in the preface, you should have a general understanding of PHP extension development. some people may think that development expansion is very complicated and complicated, this is actually not the case. in this article, we quickly enter the role and develop our first extension.

I. compile PHP

Before development, you need to prepare the PHP source code and compile it. The process is as follows:

tar -zxvf php-5.3.9.tar.gzcd php-5.3.9

I used php5.3.9. after decompression, we entered the PHP source code directory, and then we directly compiled and added php. ini:

./configure --prefix=/usr/local/webserver/php --enable-fastcgi --enable-fpm --enable-debugmake && make installcp /home/soft/php-5.3.9/php.ini-development /usr/local/webserver/php/lib/php.ini

After compilation is complete, I have not statically compiled other extensions, but I have enabled debug, which will be used later. Modify the corresponding items in php. ini. I will not go into detail here.

Now we can add PHP-related environment variables to save a lot of work:

vim /root/.bash_profile

I use root, and other users modify the contents in the corresponding user directory. in the bask_profile file, add/usr/local/webserver/php/bin/after the PATH in the file, as shown below:

PATH=$PATH:$HOME/bin:/usr/local/webserver/php/bin/

After the environment variables are set, let's check the PHP version:

OK. the compilation is complete. let's continue.

2. Typical development process

A typical extended development process is as follows:

3. define extended functions

First, define the extended functions to be completed:

This extension has only one function, that is, rewrite the PHP system function ip2long (), solve the problem that ip2long has different values in 32-bit and 64-bit systems (this problem is caused by the difference in the integer range between 32-bit and 64-bit, please google for specific reasons ).

Our new ip2long fixed return 32-bit signed integer, range-2147483648 to 2147483647, the same as the 32-bit system.

Our extension is called myip, and the function name is ip2long32.

The extended functions and names are all OK and are now developed according to the process.

IV. Formal development

1. generate a development skeleton

First, go to the source code extension directory:

cd /home/soft/php-5.3.9/ext

Next, let's take a look at the PHP extension skeleton tool ext_skel to generate a skeleton. the usage of ext_skel is as follows:

. /Ext_skel -- extname = module [-- proto = file] [-- stubs = file] [-- xml [= file] [-- skel = dir] [-- full-xml] [-- no-help] -- extname = module is the name of your extension (module name, will create a subdirectory with this name in the current directory) -- proto = file contains prototypes of functions to create (function prototype definition file) -- stubs = file generate only function stubs in file -- xml generate xml documentation to be added to phpdoc-cvs -- skel = dir path to the skeleton directory (set the directory generated by the skeleton, if this option is not set, the default value is ext/extname.) -- full-xml generate xml documentation for a self-contained extension (not yet implemented) -- no-help don't try to be nice and create comments in the code and helper functions to test if the module compiled (various help comments are not displayed in the generated code)

This time, we will use two options: -- extname = myip, which defines the extension name, and -- proto = myip. pro, which defines the extension function prototype. First, we will generate the extension function prototype file:

vim myip.pro

Add the following content:

int ip2long32(string ip)

This means that there is a function in our extension. The return value is int type and the input value is string.

Run the following command to generate the extension skeleton:

./ext_skel --extname=myip --proto=myip.pro

OK. At this time, you will find that a sub-directory myip is generated under the current PHP extension directory. go to myip and check it:

cd myipll

You will find that a bunch of files are generated, such:

Now we can proceed to step 2.

2. modify config. m4

For more information about the functions of the config. m4 file, refer to the subsequent articles for details.

Use vim to edit config. m4:

vim config.m4

Remove the dnl at the beginning of lines 16 to 18, as shown below:

The specific reason for this is described in the following article. here we exit and save config. m4 and continue to the next step.

3. encoding

Let's cheer up!

vim myip.c

Find the following location:

The figure below shows the corresponding functions generated by the extension skeleton tool based on the function prototype we provide. Here are several notes:

1. PHP_FUNCTION: a macro defined by the PHP core. it is the same as ZEND_FUNCTION and is used to define extended functions. the actually generated function name is zif_ip2long32.

2. zend_parse_parameters: because PHP is a weak type language and C is a strong type, you need to use this function to receive parameters passed in by PHP and perform type conversion, convert the PHP variable to a recognizable type in c.

The prototype of the zend_parse_parameters function is as follows:

zend_parse_parameters(int num_args TSRMLS_CC, char *type_spec, …);

Parameter description:

Num_args: number of parameters passed to the function. Generally, the macro ZEND_NUM_ARGS () is used (). TSRMLS_CC: thread security, always passing the TSRMLS_CC macro. Detailed description: http://www.54chen.com/php-tech/what-is-tsrmls_cc.html type_spec: The third parameter is a string that specifies the expected parameter type of the function...: variable list that needs to be updated with the parameter value]

Type_spec is a formatted string. its common meanings are as follows:

Parameter represents the type

B Boolean

L Integer

D Floating point

S String

R Resource

A Array

O Object instance Object

O Object instance of a specified type Object of a specific type

Z Non-specific zval of any type ~

Z zval ** type

F indicates the function and method name.

Modify the function as follows:

 PHP_FUNCTION(ip2long32) {         char *ip = NULL;         int argc = ZEND_NUM_ARGS();         int ip_len;          if (zend_parse_parameters(argc TSRMLS_CC, "s", &ip, &ip_len) == FAILURE) {                 return;         }                  int32_t ip_int32;         unsigned char ip1, ip2, ip3, ip4;                  sscanf(ip, "%hhu.%hhu.%hhu.%hhu", &ip1, &ip2, &ip3, &ip4);         ip_int32 = (int32_t)((ip1 << 24) | (ip2 << 16) | (ip3 << 8) | ip4);         RETURN_LONG(ip_int32); }

The function is complete. here there is a special RETURN_LONG (ip_int32), which is also a macro provided by the PHP kernel for returning values to PHP. The details are as follows:

Set the return value and end the function. set the return value macro return type and parameters.
RETURN_LONG (l) RETVAL_LONG (l) integer
RETURN_BOOL (B) RETVAL_BOOL (B) Boolean (1 or 0)
RETURN_NULL () RETVAL_NULL () NULL
RETURN_DOUBLE (d) RETVAL_DOUBLE (d) floating point number
RETURN_STRING (s, dup) RETVAL_STRING (s, dup) string. If dup is 1, the engine will call estrdup () to duplicate s and use copy. If dup is 0, s is used.
RETURN_STRINGL (s, l, dup) RETVAL_STRINGL (s, l, dup) is a string value of l. Similar to the previous macro, but the speed is faster because the length of s is specified.
RETURN_TRUE RETVAL_TRUE returns a boolean value of true. Note that this macro has no parentheses.
RETURN_FALSE RETVAL_FALSE returns the boolean value false. Note that this macro has no parentheses.
RETURN_RESOURCE (r) RETVAL_RESOURCE (r) resource handle.

After encoding is complete, save and exit. then we can start compiling.

4. Compile

phpize./configure --with-php-config=/usr/local/webserver/php/bin/php-configmake && make install

If the compilation is complete, the following prompt will be displayed:

Installing shared extensions:     /usr/local/webserver/php/lib/php/extensions/debug-non-zts-20090626/

Go to the directory and check whether myip. so exists. If yes, we can modify php. ini to load the so file.

5. modify php. ini

cd /usr/local/webserver/php/libvim php.ini

Modify extension_dir and add extension = myip. so

extension_dir = "/usr/local/webserver/php/lib/php/extensions/debug-non-zts-20090626/"extension = myip.so

Exit save and restart php. if Phpfpm is used, run the following command:

kill -USR2 `cat /usr/local/webserver/php/var/run/php-fpm.pid`

Check whether the extension is loaded normally:

[root@tm977 lib]# php -m|grep myipmyip

It indicates that it has been loaded normally. Finally, let's test the extended function!

6. test

php -r "var_dump(ip2long32('192.168.1.1'));"int(-1062731519)php -r "var_dump(ip2long('192.168.1.1'));"  int(3232235777)

As shown above, ip2long32 outputs 32-bit signed integers, while ip2long outputs 64-bit unsigned integers!

V. Summary

Through this development example, do you think it is very easy to develop an extension?

Yes, but don't be happy too early. In fact, it is far from that simple to develop powerful extensions. I will continue to explore the following articles, learn more complex PHP core code while developing.

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.