目录
[转]自己写PHP扩展之创建一个类,php一个类
首页 php教程 php手册 [转]自己写PHP扩展之创建一个类,php一个类

[转]自己写PHP扩展之创建一个类,php一个类

Jun 13, 2016 am 08:40 AM
函数调用

[转]自己写PHP扩展之创建一个类,php一个类

原文:http://www.imsiren.com/archives/572

比如我们要创建一个类..PHP代码如下

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Person {     public $name;     public $age;     public function __construct() {         echo "construct is running! ";     }     public function __destruct() {         echo " destruct is running!";     }     public function getproperty($key) {         echo $this->$key;     }     public function setproperty($key,$val) {         $this->$key = $val;     } }

用PHP来做,很简单..
那么用PHP扩展来写该怎么做?
OK.
1.在php_siren.h里面声明类

1 2 3 4 PHP_METHOD(Person,__construct); PHP_METHOD(Person,__destruct); PHP_METHOD(Person,setproperty); PHP_METHOD(Person,getproperty);

PHP_METHOD宏.
PHP_METHOD 等于ZEND_METHOD
这个宏接受两个参数,第一个是类名,第二个是类的方法

1 2 3 4 #define ZEND_METHOD(classname, name)    ZEND_NAMED_FUNCTION(ZEND_MN(classname##_##name)) #define INTERNAL_FUNCTION_PARAMETERS int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_v    alue_used TSRMLS_DC //最后等于 void name(int ht, zval *return_value, zval **return_value_ptr, zval *this_ptr, int return_v    alue_used TSRMLS_DC )

这个宏是用来声明我们的方法…
2.设置接收的参数
我们的方法如果需要接受参数.那么就要执行

1 2 3 ZEND_BEGIN_ARG_INFO_EX(arg_person_info,0,0,2)         ZEND_ARG_INFO(0,name) ZEND_END_ARG_INFO()

详细讲这几个宏之前先看看zend_arg_info

1 2 3 4 5 6 7 8 9 10 11 typedef struct _zend_arg_info {         const char *name; //参数名称         zend_uint name_len;//长度         const char *class_name;  //所属类名         zend_uint class_name_len;  //类名长度         zend_bool array_type_hint;         zend_bool allow_null; //允许为空         zend_bool pass_by_reference;  //引用传值         zend_bool return_reference;   //引用返回         int required_num_args;   //参数个数 } zend_arg_info;

ZEND_BEGIN_ARG_INFO_EX定义在Zend/zend_API.h

1 2 3 #define ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, return_reference, required_num_args)       \         static const zend_arg_info name[] = {                                                                                                                                           \                 { NULL, 0, NULL, 0, 0, 0, pass_rest_by_reference, return_reference, required_num_args },

很明显 声明一个zend_arg_info的数组name,然后初始化结构体的值
ZEND_ARG_INFO(0,name)的定义如下

1 #define ZEND_ARG_INFO(pass_by_ref, name)  { #name, sizeof(#name)-1, NULL, 0, 0, 0, pass_by_ref, 0, 0 },

这三个宏 执行代码 等于

1 2 3 static const zend_arg_info name[] = {                                                                                                                                                    { NULL, 0, NULL, 0, 0, 0, pass_rest_by_reference, return_reference, required_num_args }, { #name, sizeof(#name)-1, NULL, 0, 0, 0, pass_by_ref, 0, 0 }, };

3.创建zend_function_entry结构数组

1 2 3 4 5 6 7 const zend_function_entry person_functions[]={         PHP_ME(Person,__construct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)         PHP_ME(Person,__destruct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)         PHP_ME(Person,getproperty,arg_person_info,ZEND_ACC_PUBLIC)         PHP_ME(Person,setproperty,arg_person_info,ZEND_ACC_PUBLIC)         PHP_FE_END };

zend_function_entry定义如下

1 2 3 4 5 6 7 typedef struct _zend_function_entry {         const char *fname; //函数名称         void (*handler)(INTERNAL_FUNCTION_PARAMETERS);         const struct _zend_arg_info *arg_info;//参数         zend_uint num_args;//参数个数         zend_uint flags;//标示PUBLIC ?PRIVATE ?PROTECTED } zend_function_entry;

PHP_ME宏接收四个参数
1 类名,
2 方法名,
3 zend_arg_info 的参数列表,

ZEND_ACC_PUBLIC ZEND_ACC_PRIVATE ZEND_ACC_PROTECTED是我们类里面的三个访问权限
ZEND_ACC_CTOR标示构造函数
ZEND_ACC_DTOR标示析构函数
4.修改PHP_MINIT_FUNCTION
前面我们说过 PHP_MINIT_FUNCTION是在模块启动的时候执行的函数
首先创建一个全局指针 zend_class_entry *person_ce;
在PHP_MINIT_FUNCTION加入如下代码

1 2 3 4 zend_class_entry person; INIT_CLASS_ENTRY(person,"Person",person_functions); person_ce=zend_register_internal_class_ex(&person,NULL,NULL TSRMLS_CC); zend_declare_property_null(person_ce,ZEND_STRL("name"),ZEND_ACC_PUBLIC TSRMLS_CC);

1行创建一个zend_class_entry对象person.
zend_class_entry这个结构体前面也讲过 PHP内核研究之类的实现
2行初始化zend_class_entry 它执行了如下代码

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 {                                                                                                                       \         int _len = class_name_len;                                                              \         class_container.name = zend_strndup(class_name, _len);  \         class_container.name_length = _len;                                             \         class_container.builtin_functions = functions;                  \         class_container.constructor = NULL;                                             \         class_container.destructor = NULL;                                              \         class_container.clone = NULL;                                                   \         class_container.serialize = NULL;                                               \         class_container.unserialize = NULL;                                             \         class_container.create_object = NULL;                                   \         class_container.interface_gets_implemented = NULL;              \         class_container.get_static_method = NULL;                               \         class_container.__call = handle_fcall;                                  \         class_container.__callstatic = NULL;                                    \         class_container.__tostring = NULL;                                              \         class_container.__get = handle_propget;                                 \         class_container.__set = handle_propset;                                 \         class_container.__unset = handle_propunset;                             \         class_container.__isset = handle_propisset;                             \         class_container.serialize_func = NULL;                                  \         class_container.unserialize_func = NULL;                                \         class_container.serialize = NULL;                                               \         class_container.unserialize = NULL;                                             \         class_container.parent = NULL;                                                  \         class_container.num_interfaces = 0;                                             \         class_container.interfaces = NULL;                                              \         class_container.get_iterator = NULL;                                    \         class_container.iterator_funcs.funcs = NULL;                    \         class_container.module = NULL;                                                  \ }

可以对应文章>> PHP内核研究之类的实现来分析
zend_declare_property_null(person_ce,ZEND_STRL(“name”),ZEND_ACC_PUBLIC TSRMLS_CC);
创建一个值为NULL的属性
第一个参数是类名,第二个参数是 属性名称,第三个参数是属性名的长度,因为ZEND_STRL宏定义了长度,所以这里不用再传递长度.
第四个参数是属性的访问权限.
还有其他几个函数用来创建不同类型的属性

1 2 3 4 5 6 7 zend_declare_property_bool zend_declare_property_double zend_declare_property_ex zend_declare_property_long zend_declare_property_null zend_declare_property_string zend_declare_property_stringl

5.创建 php_siren.h头文件中的方法体

1 2 3 4 5 6 7 8 9 10 11 12 PHP_METHOD(Person,__construct){         php_printf("construct is running
"
); } PHP_METHOD(Person,__destruct){         php_printf("destruct is running
"
); } PHP_METHOD(Person,setproperty){   } PHP_METHOD(Person,getproperty){   }

6.最后make&& make install
编译我们的扩展,
重新启动apache.
$p=new Person();
?>
我们就能在浏览器里看到输出的内容

construct is running
destruct is running

这样 ..我们用扩展创建的一个基本类就完成了.

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

C++ 函数调用性能调优:参数传递和返回值的影响 C++ 函数调用性能调优:参数传递和返回值的影响 May 04, 2024 pm 12:57 PM

C++函数调用性能优化包括两方面:参数传递策略和返回值类型优化。参数传递方面,传递值适合小对象、不可修改参数,传递引用或指针则适合大对象、可修改参数,而传递指针速度最快。返回值优化方面,小型值可直接返回,大对象应返回引用或指针。选择合适策略能提高函数调用性能。

C++ 中如何在不同模块中调用函数? C++ 中如何在不同模块中调用函数? Apr 12, 2024 pm 03:54 PM

在C++中跨模块调用函数:声明函数:在目标模块的头文件中声明要调用的函数。实现函数:在源文件中实现函数。链接模块:使用链接器将包含函数声明和实现的模块链接在一起。调用函数:在需要调用的模块中包含目标模块的头文件,然后调用函数。

C++ 函数调用反射技术:参数传递和返回值的动态访问 C++ 函数调用反射技术:参数传递和返回值的动态访问 May 05, 2024 am 09:48 AM

C++函数调用反射技术允许在运行时动态获取函数的参数和返回值信息。使用typeid(decltype(...))和decltype(...)表达式可获取参数和返回值类型信息。通过反射,我们可以动态调用函数,并根据运行时输入选择特定的函数,实现灵活和可扩展的代码。

探索 PHP 函数调用的各种方式 探索 PHP 函数调用的各种方式 Apr 16, 2024 pm 02:03 PM

PHP函数调用共有五种方式:直接调用、通过变量调用、匿名函数、函数指针和反射。通过选择最适合情况的方法,可以优化性能和提高代码简洁性。

C++ 函数调用机制详解 C++ 函数调用机制详解 Apr 11, 2024 pm 02:12 PM

C++中的函数调用机制涉及将参数传递给函数并执行其代码,返回结果(如果存在)。参数传递有两种方式:值传递(修改在函数内部进行)和引用传递(修改反映在调用者中)。在值传递中,函数内的值修改不影响原始值(如printValue),而引用传递中的修改会影响原始值(如printReference)。

C++ 函数调用单元测试:参数传递和返回值的正确性验证 C++ 函数调用单元测试:参数传递和返回值的正确性验证 May 01, 2024 pm 02:54 PM

单元测试中验证C++函数调用时,需验证以下两点:参数传递:使用断言检查实际参数是否与预期值匹配。返回值:使用断言检查实际返回值是否等于预期值。

C++ 函数调用预处理器宏:参数传递和返回值的高级用法 C++ 函数调用预处理器宏:参数传递和返回值的高级用法 May 04, 2024 pm 04:33 PM

在C++中,预处理器宏可用于调用函数,涉及以下步骤:参数传递:宏参数用圆括号括起来,以逗号分隔。返回值:使用宏参数指定要返回的值,并赋值给变量。实战案例:通过使用宏优化查找数组中最大值索引的函数,减少了计算次数,提高了效率。

C++ 函数调用详解:参数传递机制深入分析 C++ 函数调用详解:参数传递机制深入分析 May 04, 2024 am 10:48 AM

C++函数调用有三种参数传递机制:传值调用(复制参数值),传引用调用(传递参数引用,可修改原始变量),指针传递(传递参数指针)。选择机制需考虑参数大小、是否需修改原始变量和效率。

See all articles