[php扩展开发] -- 添加类
来源:互联网 发布:网络安全保密基础知识 编辑:程序博客网 时间:2024/05/22 14:25
原文地址: http://www.djhull.com/categories.html
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.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])
- ****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[] */};//将类和方法注册到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;}
执行 命令 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.phpconstructsayingdoingdestruct
0 0
- [php扩展开发] -- 添加类
- [php扩展开发] -- 添加函数
- PHP扩展开发-定义类
- php 添加postgre扩展
- php 添加pdo_mysql扩展
- 为php添加扩展
- PHP 添加扩展
- PHP添加Xdebug扩展
- php添加pdo_mysql扩展
- php 添加mongodb扩展
- PHP添加redis扩展
- PHP添加mongodb扩展
- php添加扩展模块
- php添加mysql扩展
- php编译添加扩展
- php 添加phpredis扩展
- php 如何添加php扩展
- PHP扩展开发-创建扩展
- 数组排序之compare使用
- 20170509
- java 集合-概述
- 安卓网络编程-WebView基本用法
- printk实现分析
- [php扩展开发] -- 添加类
- 程序学徒与导师
- Python---numpy一般操作
- git管理代码报错(使用Sourcetree工具) 有子模块Submodule(未解决)
- Maven+Mybatis+Spring+SpringMVC实现分页查询(附源码)
- 1.移动端与pc端点击事件的不同:
- TCP/IP、Http、Socket的区别
- linux scp(Linux之间复制文件和目录)命令参数及用法详解
- sun.misc.unsafe类的使用