php扩展开发3--扩展类传参数

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

vim  config.m4 打开 10 和12行

PHP_ARG_WITH(person, for person support,

dnl Make sure that the comment is aligned:

[  --with-person             Include person support])

 

在php_person.h头中加上

在person.c的文件中加上(doing方法中加上  RETURN_ZVAL(array_config, 1, 0); )

// 继续修改zend_function_entry  person_functions[]  必须放在注册方法前面。防止person_functions[] 没有声明

将类和方法注册到zend中

 

phpize   ./configure   make  make install  依次执行命令

php.ini  别忘记添加。

extension=person.so

<?php
$n = new person(array('key'=>'value'));
var_dump($n->saying('error')); // 在参数声明中 saying 是空  所以会拨错
var_dump($n->saying()); // 没有传参数  所有返回空
var_dump($n->doing()); // doing方法 构造方法 赋值config 为一个数组   这里获取config属性
var_dump($n->config); //打印属性

结果

原文地址:https://www.cnblogs.com/yhl664123701/p/5311387.html