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.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])
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扩展类的写法是什么的详细内容。
