php扩展开发

下载源码

GitHub:https://github.com/php/php-src/tree/master
官网:https://www.php.net/downloads.php

扩展开发

进入源码目录中的ext目录
cd ext
生成扩展,写一个helloworld扩展
./ext_skel --extname=helloworld --proto=ini.proto
生成的扩展目录如下

.
├── CREDITS
├── EXPERIMENTAL
├── config.m4
├── config.w32
├── helloworld.c
├── helloworld.php
├── php_helloworld.h
└── tests
└── 001.phpt

实现一个自定义函数

在php_helloworld.h头文件中注册hello_world函数

PHP_FUNCTION(hello_world);

在helloworld.c文件中实现hello_world函数,注意位置

// zend引擎注册函数 zend_function_entry中添加
const zend_function_entry helloworld_functions[] = {
    PHP_FE(confirm_helloworld_compiled, NULL)       /* For testing, remove later. */
    PHP_FE(hello_world, NULL)
    PHP_FE_END  /* Must be the last line in helloworld_functions[] */
};

实现hello_world函数

PHP_FUNCTION(hello_world)
{
    char *arg;
    int len;
    char *strg;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg) == FAILURE) {
        return;
    }
    len = spprintf(&strg, 0, "String %s", arg);
    RETURN_STRINGL(strg, len);
}
安装扩展

生成编译检测脚本

phpize

编译配置检测

./configure

编译安装

make && make install

php.ini添加

extension=helloworld.so

重启php,查看phpinfo()


image.png
测试一下

使用刚才写好的扩展函数hello_world()

输出如下


image.png

你可能感兴趣的:(php扩展开发)