搜索
首页后端开发PHP7php7扩展类的写法是什么

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

php7扩展类的写法是什么

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

推荐学习:php视频教程

以上是php7扩展类的写法是什么的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:慕课网。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)