php扩展开发2--添加类
2016-03-23 12:38
615 查看
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 //运行ext_skel创建扩展的开发包
2.2 修改配置
[root@bogon ext]# vim person/config.m4dnl 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])
****dnl 在这是注释的意思。去掉dnl 就是打开 这一行的意思
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头中加上
/*定义类*/ zend_class_entry *person_ce; /** * 声明构造函数 * @param * @return */ ZEND_METHOD(person,__construct){ 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"); } /*NULL 表示不传参数 *
* ZEND_ACC_PUBLIC 说明是public 方法
* ZEND_ACC_CTOR 说明是构造函数 * ZEND_ACC_DTOR 说明是析构函数 */ const zend_function_entry person_functions[] = { ZEND_ME(person, __construct,NULL, 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[] */ }; //将类和方法注册到zend PHP_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; }
执行 命令 phpize
./configure
make
make install 或者 手动cp
改更php.ini 加上[person] extenstion=person.so
使用扩展:
[root@bogon tests]# cat test.php <?php $n = new person(); echo $n->saying(); echo $n->doing(); [root@localhost tests]# php test.php construct saying doing destruct
该文章参考与:
原链接:http://www.djhull.com/phpext/php-ext-2.html
请尊重本人劳动成功,可以随意转载但保留以下信息
作者:岁月经年
时间:2016年03月
相关文章推荐
- TP学习笔记一(tp的目录结构 , tp的输出方式)
- PHP 简介
- window下php.ini文件配置详解
- ThinkPHP静态缓存简单配置和使用方法详解
- ThinkPHP3.2加载TabLib异常
- php isset — 检测变量是否设置
- Maximum function nesting level of '100' reached, aborting!--漫谈递归:PHP里的尾递归及其优化
- php的password hashing(密码加密详解)
- php curl
- FragmentPagerAdapter和FragmentStatePagerAdapter的区别(重新温习)
- ThinkPHP项目分组配置方法分析
- php解压zip文件
- FTP服务FileZilla Server上传提示550 Permission denied
- ThinkPHP模板循环输出Volist标签用法实例详解
- php 后台数据库与前台请求结合
- PHP中的FPM是做什么的_fpm的配置与使用
- ThinkPHP模板Volist标签嵌套循环输出多维数组的方法
- Laravel artisan 命令工具
- Laravel 5.0 发布 新版本特性详解
- ThinkPHP路由机制简介