PHP中7怎么写扩展类,哪些细节要注意
Admin 2022-08-01 群英技术资讯 941 次浏览
在实际应用中,我们有时候会遇到“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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本身也是一个迭代器,当被实例化时,又需要被传递到另一个迭代器内部保存,即内部迭代InnerIterator。可通过继承IteratorIterator实现对原有迭代器功能的升级。
变量的范围即它定义的上下文背景(也就是它的生效范围)。大部分的PHP变量只有一个单独的范围。这个单独的范围跨度同样包含了include和require引入的文件。例如:<?...
swoole比原生PHP快。Swoole是PHP的异步、并行、高性能网络通信引擎。节省PHP框架和全局对象每次请求创建销毁带来的性能损耗。主要是为了补充php在网络编程方面的不足。
php中DateTime的错误处理:1、DateTime对象可以过程化地写出来的。2、提供了一种getLastErrors()方法,但是当使用面向对象的方法时,DateTime对象会以异常的形式被抛出。
本篇文章给大家介绍一下php7实现垃圾回收机制的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
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