在zval变量里IS_OBJECT类型使用zend_object_value来保存变量的,我们看一下他的具体结果。

typedef struct _zend_object_value {
zend_object_handle handle;
const zend_object_handlers *handlers;
} zend_object_value;

在扩展中创建一个我们自己的类

zend_class_entry *myext_class_ce;//保存我们创建的对象实例

static zend_function_entry myext_class_method[] = {//函数的属性和方法都在这里定义
{NULL, NULL, NULL}//固定语法
}; //在MINIT函数里
PHP_MINIT_FUNCTION(myext)
{
zend_class_entry tmp_myext_class_ce;//临时变量
INIT_CLASS_ENTRY(tmp_myext_class_ce,"myext_example_class",myext_class_method);//临时变量,类名称,类属性和方法
myext_class_ce = zend_register_internal_class(&tmp_myext_class_ce TSRMLS_CC);//在内核中注册我们的类
} /* $class = new myext_example_class();

var_dump($class);

object(myext_example_class)#6 (0) {
  }

*/

创建类的第一个函数

ZEND_METHOD(myext_example_class,hello);

//ZEND_ME为方法注册,每个方法使用一个ZEND_ME
//参数说明
//1,类名称
//2,方法名称
//3,参数,跟普通函数的一样,也是ZEND_END_ARG_INFO
//4,修饰方法或属性,后面会专门讲到
static zend_function_entry myext_class_method[] = {
ZEND_ME(myext_example_class,hello,NULL,ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}//固定语法
}; //函数定义,参数说明:
//类名称
//类方法名称
ZEND_METHOD(myext_example_class,hello){
php_printf("hello");
}

创建一个构造函数__construct()

ZEND_METHOD(myext_example_class,__construct);

//创建构造方法同普通方法基本一样,唯一的差别在于第四个参数,ZEND_ACC_CTOR,表示它是构造方法
static zend_function_entry myext_class_method[] = {
ZEND_ME(myext_example_class,hello,NULL,ZEND_ACC_PUBLIC)
ZEND_ME(myext_example_class,__construct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
{NULL, NULL, NULL}//固定语法
}; ZEND_METHOD(myext_example_class,__construct){
php_printf("我是__construct函数");
}

现在来看一下修饰方法和属性的常量,通过他们可以定义方法的访问权限,是否是抽奖函数,是否是FINAL属性等等

下面详细的罗列出了所有常量,fn_flags代表可以在定义方法时使用,zend_property_info.flags代表可以在定义属性时使用,ce_flags代表在定义zend_class_entry时候可用。当需要用到多个常量的时候,我们可以采用或运算的形式,在定义构造函数的时候,我们已经遇到了,ZEND_ACC_PUBLIC|ZEND_ACC_CTOR

#define ZEND_ACC_STATIC                     0x01     /* fn_flags, zend_property_info.flags 静态方法或属性*/
#define ZEND_ACC_ABSTRACT 0x02 /* fn_flags 抽象*/
#define ZEND_ACC_FINAL 0x04 /* fn_flags 最终方法*/
#define ZEND_ACC_IMPLEMENTED_ABSTRACT 0x08 /* fn_flags 接口抽象方法*/
#define ZEND_ACC_IMPLICIT_ABSTRACT_CLASS 0x10 /* ce_flags */
#define ZEND_ACC_EXPLICIT_ABSTRACT_CLASS 0x20 /* ce_flags */
#define ZEND_ACC_FINAL_CLASS 0x40 /* ce_flags */
#define ZEND_ACC_INTERFACE 0x80 /* ce_flags */
#define ZEND_ACC_INTERACTIVE 0x10 /* fn_flags */
#define ZEND_ACC_PUBLIC 0x100 /* fn_flags, zend_property_info.flags */
#define ZEND_ACC_PROTECTED 0x200 /* fn_flags, zend_property_info.flags */
#define ZEND_ACC_PRIVATE 0x400 /* fn_flags, zend_property_info.flags */
#define ZEND_ACC_PPP_MASK (ZEND_ACC_PUBLIC | ZEND_ACC_PROTECTED | ZEND_ACC_PRIVATE)
#define ZEND_ACC_CHANGED 0x800 /* fn_flags, zend_property_info.flags */
#define ZEND_ACC_IMPLICIT_PUBLIC 0x1000 /* zend_property_info.flags; unused (1) */
#define ZEND_ACC_CTOR 0x2000 /* fn_flags */
#define ZEND_ACC_DTOR 0x4000 /* fn_flags */
#define ZEND_ACC_CLONE 0x8000 /* fn_flags */
#define ZEND_ACC_ALLOW_STATIC 0x10000 /* fn_flags */
#define ZEND_ACC_SHADOW 0x20000 /* fn_flags */
#define ZEND_ACC_DEPRECATED 0x40000 /* fn_flags */
#define ZEND_ACC_CLOSURE 0x100000 /* fn_flags */
#define ZEND_ACC_CALL_VIA_HANDLER 0x200000 /* fn_flags */

现在我们为这个类增加一些属性。添加属性变量需要在MINIT函数中实现,在类创建完成之后,我们就可以添加属性了

    zend_class_entry tmp_myext_class_ce;
INIT_CLASS_ENTRY(tmp_myext_class_ce,"myext_example_class",myext_class_method);
myext_class_ce = zend_register_internal_class(&tmp_myext_class_ce TSRMLS_CC);
//上面是创建类,下面是添加属性
//创建普通属性和静态属性的方法是一样,只需要在第四个参数中增加ZEND_ACC_STATIC即为静态属性
//创建类常量是另外一组的函数,和普通属性不一样
zend_declare_property_null(myext_class_ce,"private_prop",sizeof("private_prop")-,ZEND_ACC_PRIVATE TSRMLS_CC);//私有权限,默认值为NULL
zend_declare_property_string(myext_class_ce,"protected_prop",sizeof("protected_prop")-,"我是默认值",ZEND_ACC_PROTECTED TSRMLS_CC);//protected权限,默认值是"我是默认值"
zend_declare_property_long(myext_class_ce,"public_prop",sizeof("public_prop")-,,ZEND_ACC_PUBLIC TSRMLS_CC);//public,默认值是100
zend_declare_property_string(myext_class_ce,"static_prop",sizeof("static_prop")-,"我是静态变量",ZEND_ACC_PUBLIC|ZEND_ACC_STATIC TSRMLS_CC);//静态变量,默认值是"我是静态变量"
zend_declare_class_constant_string(myext_class_ce,"CONSTANT_PROP",sizeof("CONSTANT_PROP") - ,"我是类常量" TSRMLS_CC);//类常量,默认值是"我是类常量"
/*

$class = new myext_example_class();
var_dump($class);
$class->hello();
var_dump(myext_example_class::$static_prop);
var_dump(myext_example_class::CONSTANT_PROP);

object(myext_example_class)#6 (3) {
["private_prop":"myext_example_class":private]=>
NULL
["protected_prop":protected]=>
string(15) "我是默认值"
["public_prop"]=>
int(100)
}
hello

string(18) "我是静态变量"
string(15) "我是类常量"

*/

在php内核中,由于操作变量的类型不同,通常同样一个功能,会有一组函数,上面我们提到的创建类属性和类常量也是有一组操作。

//以下操作都是在zend_API.h
//定义类属性
ZEND_API int zend_declare_property(zend_class_entry *ce, const char *name, int name_length, zval *property, int access_type TSRMLS_DC);
ZEND_API int zend_declare_property_ex(zend_class_entry *ce, const char *name, int name_length, zval *property, int access_type, const char *doc_comment, int doc_comment_len TSRMLS_DC);
ZEND_API int zend_declare_property_null(zend_class_entry *ce, const char *name, int name_length, int access_type TSRMLS_DC);
ZEND_API int zend_declare_property_bool(zend_class_entry *ce, const char *name, int name_length, long value, int access_type TSRMLS_DC);
ZEND_API int zend_declare_property_long(zend_class_entry *ce, const char *name, int name_length, long value, int access_type TSRMLS_DC);
ZEND_API int zend_declare_property_double(zend_class_entry *ce, const char *name, int name_length, double value, int access_type TSRMLS_DC);
ZEND_API int zend_declare_property_string(zend_class_entry *ce, const char *name, int name_length, const char *value, int access_type TSRMLS_DC);
ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *name, int name_length, const char *value, int value_len, int access_type TSRMLS_DC); //定义类常量
ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value TSRMLS_DC);
ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length TSRMLS_DC);
ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, long value TSRMLS_DC);
ZEND_API int zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value TSRMLS_DC);
ZEND_API int zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value TSRMLS_DC);
ZEND_API int zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length TSRMLS_DC);
ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value TSRMLS_DC);

接下来,我们来看一下,接口的实现,以及接口和类的继承

定义一个接口,这个过程与定义类类似

zend_class_entry *myext_interface_ce;

static zend_function_entry myext_interface_method[] = {
ZEND_ABSTRACT_ME(myext_interface_ce,int_func,NULL)//定义一个抽奖方法,这个方法可以不实现,也就是不需要存在一个ZEND_ME(myext_interface_ce,int_func)
{NULL, NULL, NULL}//固定语法
}; zend_class_entry tmp_myext_interface_ce;
INIT_CLASS_ENTRY(tmp_myext_interface_ce,"myext_example_interface",myext_interface_method);
myext_interface_ce = zend_register_internal_interface(&tmp_myext_interface_ce TSRMLS_CC);//注册接口的函数与注册类的不一样 /*

class interface_class implements myext_example_interface{
public function int_func(){
echo "我继承了接口\n";
}
}

$interface_class = new interface_class();
$interface_class->int_func();

我继承了接口

*/

我们在扩展的代码里实现继承,让myext_example_class继承myext_example_interface

ZEND_METHOD(myext_example_class,int_func);//php_myext.h

//抽象函数int_func的实现
ZEND_METHOD(myext_example_class,int_func){
php_printf("我是class继承了接口interface");
} static zend_function_entry myext_class_method[] = {
ZEND_ME(myext_example_class,hello,NULL,ZEND_ACC_PUBLIC)
ZEND_ME(myext_example_class,__construct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
ZEND_ME(myext_example_class,int_func,NULL,ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}//固定语法
}; zend_class_entry tmp_myext_interface_ce;
INIT_CLASS_ENTRY(tmp_myext_interface_ce,"myext_example_interface",myext_interface_method);
myext_interface_ce = zend_register_internal_interface(&tmp_myext_interface_ce TSRMLS_CC); zend_class_entry tmp_myext_class_ce;
INIT_CLASS_ENTRY(tmp_myext_class_ce,"myext_example_class",myext_class_method);
myext_class_ce = zend_register_internal_class(&tmp_myext_class_ce TSRMLS_CC);
zend_declare_property_null(myext_class_ce,"private_prop",sizeof("private_prop")-,ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(myext_class_ce,"protected_prop",sizeof("protected_prop")-,"我是默认值",ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_long(myext_class_ce,"public_prop",sizeof("public_prop")-,,ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(myext_class_ce,"static_prop",sizeof("static_prop")-,"我是静态变量",ZEND_ACC_PUBLIC|ZEND_ACC_STATIC TSRMLS_CC);
zend_declare_class_constant_string(myext_class_ce,"CONSTANT_PROP",sizeof("CONSTANT_PROP") - ,"我是类常量" TSRMLS_CC);
zend_class_implements(myext_class_ce TSRMLS_CC,,myext_interface_ce);//这句话表示myext_class_ce继承接口myext_interface_ce /*

$class = new myext_example_class();

$class->int_func();

我是class继承了接口interface

*/

我们再来实现一个类继承的例子

ZEND_METHOD(myext_example_class,class_func);//php_myext.h
ZEND_METHOD(myext_example_parent_class,class_func);//php_myext.h zend_class_entry *myext_parent_class_ce;//保存父类的变量 static zend_function_entry myext_parent_class_method[] = {
ZEND_ME(myext_example_parent_class,class_func,NULL,ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}//固定语法
}; zend_class_entry tmp_myext_parent_class_ce;
INIT_CLASS_ENTRY(tmp_myext_parent_class_ce,"myext_example_parent_class",myext_parent_class_method);
myext_parent_class_ce = zend_register_internal_class(&tmp_myext_parent_class_ce TSRMLS_CC);//注册一个myext_example_parent_class zend_class_entry tmp_myext_class_ce;
INIT_CLASS_ENTRY(tmp_myext_class_ce,"myext_example_class",myext_class_method);
//myext_class_ce = zend_register_internal_class(&tmp_myext_class_ce TSRMLS_CC);//正常注册类
myext_class_ce = zend_register_internal_class_ex(&tmp_myext_class_ce,myext_parent_class_ce,"myext_example_parent_class" TSRMLS_CC);//继承注册类,第一个参数是一样的,第二个是继承父类的变量,第三个是父类的名称
zend_declare_property_null(myext_class_ce,"private_prop",sizeof("private_prop")-,ZEND_ACC_PRIVATE TSRMLS_CC);
zend_declare_property_string(myext_class_ce,"protected_prop",sizeof("protected_prop")-,"我是默认值",ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_long(myext_class_ce,"public_prop",sizeof("public_prop")-,,ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(myext_class_ce,"static_prop",sizeof("static_prop")-,"我是静态变量",ZEND_ACC_PUBLIC|ZEND_ACC_STATIC TSRMLS_CC);
zend_declare_class_constant_string(myext_class_ce,"CONSTANT_PROP",sizeof("CONSTANT_PROP") - ,"我是类常量" TSRMLS_CC);
zend_class_implements(myext_class_ce TSRMLS_CC,,myext_interface_ce); ZEND_METHOD(myext_example_parent_class,class_func){
php_printf("我是父类的class_func\n");
} ZEND_METHOD(myext_example_class,class_func){
php_printf("我是子类的class_func\n");
} /*

$parent_class = new myext_example_parent_class();
$parent_class->class_func();
$class = new myext_example_class();
$class->class_func();

我是父类的class_func
我是__construct函数
我是子类的class_func

*/              

接着我们来看一下怎么样在类方法中使用类的属性变量

ZEND_METHOD(myext_example_class,read_property){
zval *private_prop,*protected_prop,$public_prop;
zval *static_prop;
zend_class_entry *ce; ce = Z_OBJCE_P(getThis()); private_prop = zend_read_property(myext_class_ce,getThis(),"private_prop",sizeof("private_prop") - , TSRMLS_CC);//获取private_prop的属性值,相当于php的$this->private_prop
php_var_dump(&private_prop, TSRMLS_CC);
protected_prop = zend_read_property(myext_class_ce,getThis(),"protected_prop",sizeof("protected_prop") - , TSRMLS_CC);
php_var_dump(&protected_prop, TSRMLS_CC); static_prop = zend_read_static_property(ce,"static_prop",sizeof("static_prop") - , TSRMLS_CC);//获取静态属性static_prop的属性值,相当于php的self::$static_prop
php_var_dump(&static_prop, TSRMLS_CC); zend_update_property_string(myext_class_ce,getThis(),"protected_prop",sizeof("protected_prop") - ,"更新protected_prop的值" TSRMLS_CC);//设置protected_prop的属性值,相当于php的$this->protected_prop = "更新protected_prop";
protected_prop = zend_read_property(myext_class_ce,getThis(),"protected_prop",sizeof("protected_prop") - , TSRMLS_CC);
php_var_dump(&protected_prop, TSRMLS_CC); zend_update_static_property_string(myext_class_ce,"static_prop",sizeof("static_prop") - ,"更新static_prop的值" TSRMLS_CC);//设置静态属性static_prop的属性值,相当于php的self::$static_prop = "更新static_prop的值";
static_prop = zend_read_static_property(ce,"static_prop",sizeof("static_prop") - , TSRMLS_CC); 

php_var_dump(&static_prop, TSRMLS_CC); }
//zend_update_property*的一组函数
ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const char *name, int name_length, zval *value TSRMLS_DC);
ZEND_API void zend_update_property_null(zend_class_entry *scope, zval *object, const char *name, int name_length TSRMLS_DC);
ZEND_API void zend_update_property_bool(zend_class_entry *scope, zval *object, const char *name, int name_length, long value TSRMLS_DC);
ZEND_API void zend_update_property_long(zend_class_entry *scope, zval *object, const char *name, int name_length, long value TSRMLS_DC);
ZEND_API void zend_update_property_double(zend_class_entry *scope, zval *object, const char *name, int name_length, double value TSRMLS_DC);
ZEND_API void zend_update_property_string(zend_class_entry *scope, zval *object, const char *name, int name_length, const char *value TSRMLS_DC);
ZEND_API void zend_update_property_stringl(zend_class_entry *scope, zval *object, const char *name, int name_length, const char *value, int value_length TSRMLS_DC); //
zend_update_static_property*的一组函数
ZEND_API int zend_update_static_property_null(zend_class_entry *scope, const char *name, int name_length TSRMLS_DC); 
ZEND_API int zend_update_static_property_bool(zend_class_entry *scope, const char *name, int name_length, long value TSRMLS_DC);
ZEND_API int zend_update_static_property_long(zend_class_entry *scope, const char *name, int name_length, long value TSRMLS_DC);
ZEND_API int zend_update_static_property_double(zend_class_entry *scope, const char *name, int name_length, double value TSRMLS_DC);
ZEND_API int zend_update_static_property_string(zend_class_entry *scope, const char *name, int name_length, const char *value TSRMLS_DC);
ZEND_API int zend_update_static_property_stringl(zend_class_entry *scope, const char *name, int name_length, const char *value, int value_length TSRMLS_DC);
ZEND_API int zend_update_static_property(zend_class_entry *scope, const char *name, int name_length, zval *value TSRMLS_DC);

php扩展开发-面向对象的更多相关文章

  1. iOS开发系列--App扩展开发

    概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...

  2. PHP 扩展开发(将自己的一些代码封装成PHP扩展函数)

    今天时间不多,先给个地址,能搜到我这篇blog的朋友先看看我最近在看的一些文章.资料吧: 我的环境是 lnmp1.1 的 (LNMP一键安装包),所以要进行PHP扩展开发首先应该对环境配置和shell ...

  3. 关于PHP扩展开发(收藏)

    一.Linux shell命令: ls –lh    查看文件大小 du –a    查看文件及文件夹大小 -------------------------- nginx ------------- ...

  4. postgres扩展开发

    扩展开发的基本组成 demo--1.0.sql demo.c demo.control Makefile demo.c当中包含了自定义函数的实现,纯C语言,目录下可包含多个.c文件.demo-1.0. ...

  5. 【转发】NPAPI学习(Firefox和Chrome扩展开发 )

    NPAPI学习(Firefox和Chrome扩展开发 ) 2011-11-08 14:41:02 by [6yang], 1172 visits, 收藏 | 返回 Firefox和Chrome扩展开发 ...

  6. Chrome扩展开发之二——Chrome扩展中脚本的运行机制和通信方式

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...

  7. PHP扩展开发相关总结

    1.线程安全宏定义 在TSRM/TSRM.h文件中有如下定义 #define TSRMLS_FETCH() void ***tsrm_ls = (void ***) ts_resource_ex(0, ...

  8. Firefox扩展开发

    Firefox扩展开发 (插件开发) Extension开发 入门教程 5步走 五步走   首先需要知道什么是"Firefox插件".这里说的"插件"只是一个通 ...

  9. Chrome浏览器扩展开发系列之十四

    Chrome浏览器扩展开发系列之十四:本地消息机制Native messaging 时间:2015-10-08 16:17:59      阅读:1361      评论:0      收藏:0    ...

随机推荐

  1. Vue.js基础语法(二)组件

    vue学习的一系列,全部来自于表哥---表严肃,是我遇到过的讲课最通透,英文发音最好听的老师,想一起听课就去这里吧 https://biaoyansu.com/i/hzhj1206 把一段经常要用的东 ...

  2. intellijidea课程 intellijidea神器使用技巧 3-4 alter+enter

    alter enter ==> 创建函数 fi() ==> alter enter

  3. Django Rest Framework框架源码流程

    在详细说django-rest-framework源码流程之前,先要知道什么是RESTFUL.REST API . RESTFUL是所有Web应用都应该遵守的架构设计指导原则. REST是Repres ...

  4. #CSS的盒子模型、元素类型

    CSS的盒子模型.元素类型   本文首先介绍了CSS元素的统一内部结构模型,盒子模型:然后介绍了CSS元素基于不同分类标准定义的元素类型,包括基于不同内容设置方式定义的replaced元素和non-r ...

  5. better-scroll 遇到的问题 2

    问题的描述: 在歌曲列表页面使用了scroll插件,搜索了很多歌曲,页面出现滚动,选择播放一首歌曲,弹出播放器,将播放器最小化,页面回到歌曲列表,并且页面的底部出播放歌曲的信息(在没有播放歌曲的时候是 ...

  6. Oracle笔记4-pl/sql-分支/循环/游标/异常/存储/调用/触发器

    一.pl/sql(Procedure Language/SQL)编程语言 1.概念 PL/SQL是Oracle数据库对SQL语句的扩展.在普通SQL语句的使用上增加了编程语言的特点,所以PL/SQL把 ...

  7. echarts渲染一个风向图

    今天给大家说一个用echarts渲染一个风向图,这里图上其实有三个要素,风向,风级和能见度,主要还是讲讲代码里面的坑 1.风向图标方向修改以及设置 var ownData = echarts.util ...

  8. CSS改变placeholder的颜色

    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #a1a1a1; } ::-moz-placeholder { /* Mozilla ...

  9. iDempiere 使用指南 插件安装过程

    Created by 蓝色布鲁斯,QQ32876341,blog http://www.cnblogs.com/zzyan/ iDempiere官方中文wiki主页 http://wiki.idemp ...

  10. Sharepoint 2013企业内容管理学习笔记终章

    说完了半自动化内容管理&全自动化内容管理,下面我们来说另外一个企业内容管理的东东吧 企业内容记录化 这个企业内容记录化,其实是我起的名字了,在sharepoint里面它叫做声明记录 这个声明记 ...