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()
在这里插入图片描述

测试一下

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

<?php
var_dump(hello_world("hello world"));

输出如下
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值