栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php7 类的方法传参

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php7 类的方法传参



1.需要实现的细节
  • 实现一个person类 ,实现一个doing方法和saying方法

  • 在构造方法中传递一个数组,在doing中打印此数组

  • saying方法中,构建一个空数组,返回,不需要传参。

2.class扩展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头中加上

ZEND_BEGIN_ARG_INFO_EX(global_config_arg, 0, 0, 1)    ZEND_ARG_INFO(0, global_config)ZEND_END_ARG_INFO()ZEND_METHOD(person,__construct){    zval *array_config;    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array_config) == FAILURE) {        RETURN_NULL();        }    //php_var_dump( &array_config,1 TSRMLS_CC);    zend_update_property(person_ce, getThis(), "config", sizeof("config")-1, array_config TSRMLS_CC);}ZEND_METHOD(person,__destruct){    zend_printf("destructn");}ZEND_METHOD(person,doing){    //array_init(return_value);    zval  *array_config;    zval  rv; //php >=7.0    array_config = zend_read_property(person_ce, getThis(), "config", sizeof("config")-1, 0, &rv TSRMLS_DC);    if( Z_TYPE_P(array_config) == IS_NULL || Z_TYPE_P(array_config) != IS_ARRAY ){        //zend_error(E_ERROR, "framework config error!");        RETURN_FALSE;    }   //php_var_dump(&array_config, 1 TSRMLS_CC);    RETURN_ZVAL(array_config, 1, 0);   //zend_printf("doingn");}ZEND_METHOD(person,saying){    if (zend_parse_parameters_none() == FAILURE) {        RETURN_FALSE;    }    array_init(return_value);    //zend_printf("sayingn");}//这个函数需要加上声明,去掉了没用的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  };//将类和方法注册到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'value'));var_dump($n->saying('error'));var_dump($n->saying());var_dump($n->doing());[root@localhost tests]# php test.phpPHP Warning:  person::saying() expects exactly 0 parameters, 1 given in /usr/local/src/php-7.0.3/ext/person/tests/test.php on line 5bool(false)array(0) {}array(1) {  ["key"]=>  string(5) "value"}
3.引用宏说明3.1 定义参数宏 Zend/zend_API.h
[root@bogon php-7.0.3]# grep -A 10  'ZEND_BEGIN_ARG_INFO_EX' ./Zend#define ZEND_ACC_STATIC                 0x01#define ZEND_ACC_ABSTRACT               0x02#define ZEND_ACC_FINAL                  0x04#define ZEND_ACC_IMPLEMENTED_ABSTRACT           0x08#define ZEND_ACC_IMPLICIT_ABSTRACT_CLASS        0x10#define ZEND_ACC_EXPLICIT_ABSTRACT_CLASS        0x20#define ZEND_ACC_INTERFACE                          0x40#define ZEND_ACC_TRAIT                                          0x80#define ZEND_ACC_ANON_CLASS                 0x100#define ZEND_ACC_ANON_BOUND                 0x200#define ZEND_ACC_PUBLIC         0x100#define ZEND_ACC_PROTECTED      0x200#define ZEND_ACC_PRIVATE        0x400#define ZEND_ACC_PPP_MASK  (ZEND_ACC_PUBLIC | ZEND_ACC_PROTECTED | ZEND_ACC_PRIVATE)#define ZEND_ACC_CHANGED        0x800#define ZEND_ACC_IMPLICIT_PUBLIC        0x1000#define ZEND_ACC_CTOR           0x2000#define ZEND_ACC_DTOR           0x4000#define ZEND_ACC_CLONE          0x8000
  • 请尊重本人劳动成功,可以随意转载但保留以下信息

  • 作者:岁月经年

  • 时间:2016年03月


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/228288.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号