php扩展开发1--添加函数

目标:便携php扩展 要求实现 输出hello word

首先用的是php7.0.3   centos7.1或者centos6.+

1.1 RPM安装PHP
  • rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  • yum install php70w
  • php -v 看一下 7.0.3
  • php -m 看一下 php70w-devel, php70w-opcache模块安装没有,没有的话安装一下
  • PS:如果你的centos 是选择的低版本 这个(https://mirror.webtatic.com/yum/el7/webtatic-release.rpm)连接的中el7也得修改。
1.2 下载php源码包  注意版本
  • http://hk1.php.net/distributions/php-7.0.3.tar.gz (wget命令)
  • 把源码放在/usr/local/src/下解压

2.第一个扩展

2.1 输入:

[root@bogon ext]# cd /usr/local/src/php-7.0.3/ext

[root@bogon ext]# ./ext_skel --extname-hello   

此时会生成:

cd hello/

ls  会看到几个文件

config.m4  config.w32  CREDITS  EXPERIMENTAL  hello.c  hello.php  php_hello.h  tests

2.2修改配置

[root@bogon ext]# vim hello/config.m4

  • dnl PHPARGWITH(hello, for hello support,
  • dnl Make sure that the comment is aligned:
  • dnl [ --with-hello Include hello support])
  • 更改为:
  • PHPARGWITH(hello, for hello support,
  • dnl Make sure that the comment is aligned:
  • [ --with-hello Include hello support])

2.3 代码实现

/*新增方法  该方法 必须放在  const zend_function_entry       
 *    hello_functions[] 上面
*/
     PHP_FUNCTION(hello)
     {
       zend_string *strg;
       strg = strpprintf(0, "hello word");
       RETURN_STR(strg);
     }

      const zend_function_entry hello_functions[] = {
          PHP_FE(hello,   NULL)       /* For testing, remove later. */
          PHP_FE(confirm_hello_compiled,  NULL)//这个可以删除了。
         /* For testing, remove later. */
          PHP_FE_END  /* Must be the last line in hello_functions[] */
      }   

执行命令  phpize ( linux 下 用phpize 给php 动态添加扩展。)

如果phpize  执行失败   可能是缺少 gcc  (yum  install  gcc)

phpize  成功之后会生成一些文件 

此时进行编译 ./configure

make  此时会有一个 modus 的文件夹  文件夹中会有2个文件  

hello.la  hello.so

make install 或者 直接运行命令(cp modules/hello.so  /usr/lib64/php/modules)

同时 改更php.ini 加上

[hello]

extenstion=hello.so

扩展使用 

[root@bogon hello]#  ls

会有一个 hello.php  文件

[root@bogon tests]# cat test.php
<?php 

echo hello();
echo "
";

[root@bogon tests]# php test.php
hello word  

输出了 hello word 

此文转载于  原连接 http://www.djhull.com/phpext/php-ext-1.html

  • 请尊重本人劳动成功,可以随意转载但保留以下信息
  • 作者:岁月经年
  • 时间:2016年03月
原文地址:https://www.cnblogs.com/yhl664123701/p/5310094.html