当前位置: 移动技术网 > IT编程>开发语言>PHP > Linux 下编写一个 PHP 扩展

Linux 下编写一个 PHP 扩展

2019年11月22日  | 移动技术网IT编程  | 我要评论

    假设需求

开发一个叫做 helloword 的扩展。

扩展里有一个函数,helloword()。

 

  echo helloword('tom');
    //返回:hello world: tom

 


    本地环境

php版本:5.6.9

系统:linux centos release 6.5 (final)

    最终效果



    实现流程

第一步:

    进入到本地的php目录执行:
     

    cd /root/soft/src/php-5.6.9
    cd ext
    ./ext_skel --extname=helloword
    cd helloword
    vi config.m4
     
    搜索:dnl otherwise use enable 将下面修改成:
     
    php_arg_enable(helloworld, whether to enable helloworld support,
    [  --enable-helloworld           enable helloworld support])
     
    if test "$php_helloworld" != "no"; then
     
    ...

 


如图:




第二步:

 

  vi php_helloworld.h
     
    搜索:extern zend_module_entry 新增一行:
     
    php_function(helloworld);

 


如图:



第三步:

 

   vi helloworld.c
     
    搜索:const zend_function_entry helloworld_functions[] 新增一行:
     
    php_fe(helloworld, null)

 



如图:



 

   在 helloworld.c 底部新增一个方法
     
    php_function(helloworld)
    {
        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, "hello world: %s", arg);
        return_stringl(strg, len, 0);
    }

 


如图:



第四步:

 

  //编译安装
    cd /root/soft/src/php-5.6.9/ext
    /usr/local/php/bin/phpize #用phpize生成configure配置文件
    ./configure --with-php-config=/usr/local/php/bin/php-config   #配置
    make  #编译
    make install  #安装

 



第五步:

 

   //修改php.ini
    extension="helloworld.so"   #名称为安装扩展的名称

 


第六步:

重启环境。

完成上面的步骤,简单的 helloworld 扩展就ok了。

大家可以根据自己的需求,开发满足自己的扩展。

比如,可以开发一些扩展类,扩展方法,等等。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网