自己动手写php扩展

1.下载最新版php源码

git clone https://github.com/php/php-src

2.切换到ext目录

cd php-src
cd ext
 

3.利用php-src提供的工具生成扩展的骨架

./ext_skel.php --ext test

便可以在当前目录查看到test文件夹--扩展的文件夹

./ext_skel.php具体怎么使用,可在当前目录输入

./ext_skel.php --help

 

4.如果是需要把扩展编译进php,则选择静态重编译;否则选择动态加载so的方式。根据扩展的载入方式,修改config.m4.

静态重编译:去掉PHP_ARG_ENABLE前的注释dnl,注释掉PHP_ARG_WITH

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

dnl Otherwise use enable:

PHP_ARG_ENABLE(test, whether to enable test support,
dnl Make sure that the comment is aligned:
[  --enable-test          Enable test support], no)

动态载入方式:去掉PHP_ARG_WITH前的注释dnl,注释掉PHP_ARG_ENABLE

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

dnl Otherwise use enable:

dnl PHP_ARG_ENABLE(test, whether to enable test support,
dnl Make sure that the comment is aligned:
dnl [  --enable-test          Enable test support], no)

我这里选择动态载入的方式。此时php已经编译安装,我这里在此基础上添加扩展

5.在当前目录test目录下执行phpize,生成编译的所需脚本。确保已安装php-dev工具。权限若不够,使用sudo phpize

phpize    

6.添加自定义函数 test_myfunc


//1.添加test_func函数定义(一个可选参数)
/* {{{ void test_myfunc()
 */
PHP_FUNCTION(test_myfunc)
{
	char *var = "myfunc";
	size_t var_len = sizeof("myfunc") - 1;
	zend_string *retval;

	ZEND_PARSE_PARAMETERS_START(0, 1)
		Z_PARAM_OPTIONAL
		Z_PARAM_STRING(var, var_len)
	ZEND_PARSE_PARAMETERS_END();

	retval = strpprintf(0, "Hello %s", var);

	RETURN_STR(retval);
}
/* }}} */


//2.定义test_myfunc参数结构
/* {{{ arginfo   
 */
//......
ZEND_BEGIN_ARG_INFO(arginfo_test_myfunc, 0)
	ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()

//......
/* }}} */



//3.添加扩展支持的函数列表
/* {{{ test_functions[]
 */
static const zend_function_entry test_functions[] = {
	PHP_FE(test_test1,		arginfo_test_test1)
	PHP_FE(test_test2,		arginfo_test_test2)
	PHP_FE(test_myfunc,		arginfo_test_myfunc) //!!!此处为添加的部分
	PHP_FE_END
};
/* }}} */

7.   编译生成so文件

./configure
make

 

8.make之后会在当前目录test目录下生成modules文件夹,modules文件夹下的test.so即使我们动态生成库文件。

9.把test.so文件拷贝到PHP的extensions目录里,我这里是通过phpinfo的输出数据获取的extension_dir目录

//phpinfo.php文件内容
<?php
echo phpinfo();


//执行phpinfo.php文件,获取extension_dir目录
php ~/phpinfo.php  | grep extension_dir

10.修改php.ini文件,添加extension=test.so

//执行步骤9中的phpinfo.php文件,获取Loaded Configuration File(php.ini)目录
php ~/phpinfo.php  | grep 'Loaded Configuration File'


sudo vi /etc/php/7.2/cli/php.ini 

;extension=pgsql
;extension=shmop
extension=test.so ;.so可要可不要
; The MIBS data available in the PHP distribution must be installed.
; See http://www.php.net/manual/en/snmp.installation.php
;extension=snmp

;extension=soap

11.自己写个脚本测试

//test.php
<?php
echo test_myfunc("func1\r\n");//预期 Hello func1 换行
echo test_myfunc("");//预期Hello 
echo test_myfunc();//预期Hello myfunc,扩展里面有默认值myfunc

php test.php
//结果
Hello func1
Hello Hello myfunc

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值