PHP中7怎么写扩展类,哪些细节要注意
Admin 2022-08-01 群英技术资讯 970 次浏览
在实际应用中,我们有时候会遇到“PHP中7怎么写扩展类,哪些细节要注意”这样的问题,我们该怎样来处理呢?下文给大家介绍了解决方法,希望这篇“PHP中7怎么写扩展类,哪些细节要注意”文章能帮助大家解决问题。
1.需要实现的细节
实现一个person类
实现一个doing方法和saying方法
2.第一个扩展
2.1创建类的扩展:
[root@bogon ext]# cd /usr/local/src/php-7.0.3/ext [root@bogon ext]# ./ext_skel --extname=person
2.2 修改配置
[root@bogon ext]# vim person/config.m4 dnl PHPARGWITH(person, for person support, dnl Make sure that the comment is aligned: dnl [ --with-person Include person support])
更改为:
PHPARGWITH(person, for person support, dnl Make sure that the comment is aligned: [ --with-person Include person support])
2.3 实现代码
在php_person.h头中加上
extern zend_class_entry *person_ce; PHP_METHOD(person_ce,__construct); PHP_METHOD(person_ce,saying); PHP_METHOD(person_ce,doing);
在person.c头中加上
/** * 声明构造函数 *
@param * @return */
ZEND_METHOD(person,__construct){
zval *pThis;
pThis = getThis();
zend_printf("construct\n");
}
/** * 声明析造函数 *
@param * @return */
ZEND_METHOD(person,__destruct){
zend_printf("destruct\n");
}
ZEND_METHOD(person,doing){
zend_printf("doing\n");
}
ZEND_METHOD(person,saying){
zend_printf("saying\n");
}
//这个函数需要加上声明,去掉了没用的test函数const zend_function_entry person_functions[] = {
ZEND_ME(person, __construct, global_config_arg, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
ZEND_ME(person,doing,NULL,ZEND_ACC_PUBLIC) ZEND_ME(person,saying,NULL,ZEND_ACC_PUBLIC)
ZEND_ME(person,__destruct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
PHP_FE_END
/* Must be the last line in person_functions[] */
};
//将类和方法注册到zendPHP_MINIT_FUNCTION(person){
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "person", person_functions);
person_ce = zend_register_internal_class(&ce TSRMLS_CC);
zend_declare_property_null(person_ce,"saying",strlen("saying"),ZEND_ACC_PUBLIC);
zend_declare_property_null(person_ce,"doing",strlen("doing"),ZEND_ACC_PUBLIC);
return SUCCESS;
}2.4 编译
* [root@bogon hello]# [root@localhost person]# ./configure && make && make install
2.5 扩展安装
改更php.ini 加上
[person] extenstion=person.so
2.6 扩展使用
[root@bogon tests]# cat test.php <?php $n = new person(); echo $n->saying(); echo $n->doing(); [root@localhost tests]# php test.phpconstructsayingdoingdestruct
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
关于PHP中的对象序列化这件事儿,之前我们在很早前的文章中已经提到过 __sleep() 和 __weakup() 这两个魔术方法。今天我们介绍的则是另外一个可以控制序列化内容的方式,那就是使用 Serializable 接口。它的使用和上述两个魔术方法很类似,但又稍有不同。
这篇文章主要介绍了Laravel5.1 框架Request请求操作常见用法,结合实例形式分析了Laravel5.1 框架Request请求操作常见的属性和方法,及相关操作注意事项,需要的朋友可以参考下
本篇文章是使用PHPStorm编辑器开发Laravel应的详解,给大家介绍了关于利用PHPStorm如何开发Laravel应用的相关资料,文中通过示例代码介绍的非常详细。
1.获取数组所有的键或值:array_keys() array_values();2.交换数组中键和值的位置,若重复前面的会被后面的覆盖:array_flip()...
特殊的NULL值表示一个变量没有值。NULL类型唯一可能的值就是NULL。在下列情况下一个变量被认为是NULL:被赋值为NULL。尚未被赋值。被unset()。语法NU...
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008