PHP中7怎么写扩展类,哪些细节要注意
Admin 2022-08-01 群英技术资讯 718 次浏览
在实际应用中,我们有时候会遇到“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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
打开PhpStorm,打开一个本地项目。依次找到Tools—>Deployment—>Cofiguration,点击进入。依次填入项目名称,SFTP主机地址,用户名,密码,项目的浏览器访问地址。
今天小编就为大家分享一篇关于PHP children()函数讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
在php5的对象编程经常提到的一个关键点是“默认情况下对象是通过引用传递的”。但其实这不是完全正确的。下面通过一些例子来说明。php的引用是别名,就是两个不同的变量...
本篇文章给大家介绍一下PHP7中的“?”与“??”的区别。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
在本篇文章里小编给大家整理的是关于php服务器用什么系统的相关知识点内容,有兴趣的朋友们跟着学习参考下。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008