快速开发一个PHP扩展(SO组件)教程
本文通过非常快速的方式讲解了如何制作一个PHP 5.2 环境的扩展(PHP Extension),希望能够在图文的方式下让想快速学习的朋友了解一下制作过程。
需求:比如开发一个叫做 lanhaicode 的扩展,扩展里就一个函数 lanhai_test(),输入一个字符串,函数返回:Your input string: xxxxx。
要求:了解C/C++编程,熟悉PHP编程
环境:下载一份php对应版本的源码,我这里是 php-5.2.17,先正常安装php,假设我们的php安装在 /usr/local/php 目录,源码在 /root/soft/php/php-5.2.17/,现在开始!
php-5.2.17下载地址:
http://blog.lrenwang.com/down/soft/php-5.2.17.tar.bz2
解压: tar -vxjf php-5......tar.bz2
步骤一:生成扩展框架
cd /root/soft/php/php-5.2.17/ext ./ext_skel --extname=lanhaicode cd /root/soft/php/php-5.2.17/ext/lanhaicode vi config.m4
PHP_ARG_ENABLE(lanhaicode, whether to enable lanhaicode support, [ --enable-lanhaicode Enable lanhaicode support])
vi php_lanhaicode.h
PHP_FUNCTION(lanhai_test);
vi lanhaicode.c
PHP_FE(lanhaicode, NULL)
PHP_FUNCTION(lanhai_test)
{
char *arg = NULL;
int arg_len, len;
char *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Your input string: %s\n", arg);
RETURN_STRINGL(strg, len, 0);
}
cd /root/soft/php/php-5.2.17/ext/lanhaicode /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make make test make install
vi /usr/local/php/lib/php.ini
<?php
echo "<pre class="brush:php;toolbar:false;">";
print_r(get_loaded_extensions());
print_r(get_extension_funcs('lanhaicode'));
echo lanhai_test('My first php extension');
echo "
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号