PHP扩展开发--第一扩展(扩展开发流程)

Posted by hiho on April 18, 2017

想要实现内容

编写一个扩展,里面还有一个hello_world方法,传入一个数值n,调用后返回n个Hello World!字符串的拼接字符串。

函数定义文件

为扩展建立函数的第一步是写一个函数定义文件,该函数定义文件定义了扩展对外提供的函数原形。该例中,定义函数只有一行函数原形

string hello_world(int n)

函数定义文件的一般格式是一个函数一行。你可以定义可选参数和使用大量的PHP类型,包括: bool, float, int, array等。 保存为my_functions.def文件至PHP原代码目录树下。

利用官方工具ext_skel生成扩展框架

./ext_skel --extname=my_functions --proto=my_functions.def

运行后结果: ext_skel

生成的my_functions目录: my_functions_dir

PHP扩展动态编译与静态编译

1.动态编译

动态编译就是指已经安装了系统已经安装了php,但是要有些扩展是后面才加上的,但有不想重新安装php(如自己编写的php扩展)。
最后编译成.so文件,然后加到php.ini文件上

2.静态编译

静态编译也就安装php的时候连带安装php扩展

–enable-×××和–with-×××

  • --enable-×××配置开关到PHP编译系统里
  • --with-××× 开关用于那些需要用户指定相关C库路径的扩展

然后编辑my_functions/config.m4

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

修改扩展内容

编译扩展

cd my_functions 
phpize
./configure --with-php-config=/usr/bin/php-config
make#已经编译生成.so扩展文件到modules目录下
make install#主要复制modules到系统扩展目录上/usr/lib/php/20151012

添加编译后的扩展

make install后添加到 php.ini

extension="/usr/lib/php/20151012/myfunctions.so"

—End—

迭代

  • 2016年04月18日 11:14 初稿

参考文章