php7 扩展类的写法[2]
2016-03-30 21:27
1126 查看
来自 www.djhull.com
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 扩展安装
1. 改更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.phpconstruct saying doing destruct
请尊重本人劳动成功,可以随意转载但保留以下信息
作者:岁月经年
时间:2016年03月
相关文章推荐
- 一个简单的asp数据库操作类
- C#实现用于操作wav声音文件的类实例
- C#类的多态性详解
- C#、ASP.NET通用扩展工具类之TypeParse
- Lua编程示例(二):面向对象、metatable对表进行扩展
- Windows Powershell扩展类型系统
- 在线管理数据库 类
- Lua面向对象之类和继承浅析
- Lua中类的实现原理探讨(Lua中实现类的方法)
- Lua中的类编程代码实例
- C#常用目录文件操作类实例
- c# 类和成员的修饰详细介绍
- C#中实现判断某个类是否实现了某个接口
- C#类的创建与初始化实例解析
- C#、ASP.NET通用扩展工具类之LogicSugar
- C#基础语法:结构和类区别详解
- 深入c# 类和结构的区别总结详解
- c#结构和类的相关介绍
- C#中类与结构的区别实例分析