Home > Backend Development > PHP Tutorial > PHP study notes PHP extension development tutorial_PHP tutorial

PHP study notes PHP extension development tutorial_PHP tutorial

WBOY
Release: 2016-07-13 17:07:39
Original
1001 people have browsed it

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
 代码如下 复制代码

function tao_string($str){
$result = $str;
return $result;
}

function tao_string($str){

$result = $str;
return $result;

}

The first step is to generate code

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/
./ext_skel --extname=tao --proto=tao.skel
cd tao/

string tao_string(string str)

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
 代码如下 复制代码

dnl PHP_ARG_WITH(tao, for tao support,
dnl Make sure that the comment is aligned:
dnl [  --with-tao             Include tao support])

修改为

PHP_ARG_WITH(tao, for tao support,
Make sure that the comment is aligned:
[  --with-tao             Include tao support])

cd tao/


At this time, the extended code framework of tao has come out.
The second step is to modify the configuration
 代码如下 复制代码

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);
}

Then modify the config.m4 file to delete the first dnl of the three lines 10, 11, and 12, that is,

The code is as follows Copy code

dnl PHP_ARG_WITH(tao, for tao support, dnl Make sure that the comment is aligned:
 代码如下 复制代码

/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make

dnl [ --with-tao ] Include tao support]) was changed to PHP_ARG_WITH(tao, for tao support, Make sure that the comment is aligned: [ --with-tao                       Include tao support])
The third step is to implement the function Modify the source code tao.c file Find and modify the tao_string function to
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 fourth step, compile the extension After saving, start compiling
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.

cp modules/tao.so /usr/local/php/ext/ vim /usr/local/php/etc/php.ini
The code is as follows
 代码如下 复制代码

cp modules/tao.so /usr/local/php/ext/
vim /usr/local/php/etc/php.ini
extension=/usr/local/php/ext/tao.so #在php.ini文件最后增加这一行
service php-fpm restart #重启PHP服务
cp tao.php /data/www/wwwroot/default/

Copy code

extension=/usr/local/php/ext/tao.so #Add this line at the end of the php.ini file service php-fpm restart #Restart PHP service

cp tao.php /data/www/wwwroot/default/

Next, you can access the tao.php file and test it http://www.bkjia.com/PHPjc/629901.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/629901.htmlTechArticlePHP extension development is not something that all developers can operate. Let me demonstrate the implementation process of PHP extension development. , students can enter for reference if necessary. Let's first assume that...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template