首页 > 后端开发 > PHP7 > 正文

php7扩展类的写法是什么

醉折花枝作酒筹
发布: 2021-06-30 09:16:37
转载
2730人浏览过

php7中,有许许多多的扩展类,今天我们就以person类为例实现doing方法和saying方法,有需要的小伙伴可以参考一下。

php7扩展类的写法是什么

1.需要实现的细节

实现一个person类

实现一个doing方法和saying方法

立即学习PHP免费学习笔记(深入)”;

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
登录后复制

推荐学习:php视频教程

以上就是php7扩展类的写法是什么的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
相关标签:
来源:慕课网网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号