php7 ext skel_php7 扩展类的写法[2]

来自 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.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 扩展安装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月

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值