PHP extension development is not something that all developers can operate. Below I will demonstrate the implementation process of PHP extension development. Students can refer to it if necessary.
Let’s first assume that we need such an extension and provide a function called tao_string. Its main function is to return a period of characters. The corresponding PHP code may be like this:
The code is as follows | Copy code | ||||
$result = $str;
|
In order to extend the convenience of development, PHP provides a tool ext_skel similar to a code generator. For details, please refer to the official instructions of php.net (https://svn.php.net/repository/php/php-src/trunk/README. EXT_SKEL).
Create a file tao.skel with the content
代码如下 | 复制代码 |
cd MooENV/src/php-5.3.8/ext/ |
Just tell ext_skel that there is a function called tao_string in the extension we want to do. Then execute
The code is as follows | Copy code | ||||
cd MooENV/src/php-5.3.8/ext/ ./ext_skel --extname=tao --proto=tao.skel
|
代码如下 | 复制代码 |
PHP_FUNCTION(tao_string) if (zend_parse_parameters(argc TSRMLS_CC, "s", &str, &str_len) == FAILURE) str_len = spprintf(&result, 0, "wo shi %.78s", str); |
The code is as follows | Copy code | ||||
dnl PHP_ARG_WITH(tao, for tao support, dnl Make sure that the comment is aligned:
|
The code is as follows | Copy code |
PHP_FUNCTION(tao_string) { char *str = NULL; int argc = ZEND_NUM_ARGS(); int str_len; char *result; if (zend_parse_parameters(argc TSRMLS_CC, "s", &str, &str_len) == FAILURE) return; str_len = spprintf(&result, 0, "wo shi %.78s", str); RETURN_STRINGL(result, str_len, 0); } |
The code is as follows | Copy code |
/usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make |
Step 5, add extensions
At this time, if everything goes well, the extension is already in modules/tao.so. The following is to add this extension to PHP so that our PHP program can call it.
The code is as follows
|
Copy code
|
||||
cp modules/tao.so /usr/local/php/ext/ |
cp tao.php /data/www/wwwroot/default/
www.bkjia.com