PHP源码分析-面向对象(一)

来源:互联网 发布:php网页输出原代码 编辑:程序博客网 时间:2024/06/06 13:56
在PHP中,面向对象与Class是密切相关的,来看看在内核中如何实现Class的接口、继承、私有、受保护、公开等特性。


一、创建类
zend_class_entry是内核中定义的一个结构体,是PHP中类与对象的基础结构类型。
如何在扩展中定义myclass类,并能够在PHP中能够实例化?

PHP中看起来像这样:

<?php$obj = new myclass();?>

内核中:
zend_class_entry *myclass;//定义全局指针变量,指向myclass类。static zend_function_entry myclass_method[] = {//myclass中的方法{NULL, NULL, NULL}}PHP_MINIT_FUNCTION(extension_name){zend_class_entry class;INIT_CLASS_ENTRY(class, "myclass", myclass_method);myclass = zend_register_internal_class(&class TSRMLS_CC);return SUCCESS;}

某个类的zend_class_entry会经常用到,所以我们一般会把它保存在一个全局变量里,供扩展中其它地方的程序使用.


对应上述myclass类的实现,在PHP中的代码:
<?phpclass myclass{}?>

这个类没有任何属性和方法。


二、创建类的属性

在介绍类的属性之前,先了解类中的public、protected、private在内核中的表示。

#define ZEND_ACC_STATIC                     0x01     /* Static */#define ZEND_ACC_PUBLIC                     0x100    /* Public */#define ZEND_ACC_PROTECTED                  0x200    /* Protected */#define ZEND_ACC_PRIVATE                    0x400    /* Private */

定义类的属性可以使用内核提供的zend_declare_property_*系列函数,同时需要三个信息:
属性名称、属性默认值、属性访问权限。

<?phpclass myclass{private my_var="this is property";}?>

内核中:
zend_class_entry *myclass;static zend_function_entry myclass_method[] = {{NULL, NULL, NULL}}PHP_MINIT_FUNCTION(extension_name){zend_class_entry class;INIT_CLASS_ENTRY(class, "myclass", myclass_method);myclass = zend_register_internal_class(&class TSRMLS_CC);zend_declare_property_string(myclass, "my_var", strlen("my_var"), "this is property", ZEND_ACC_PRIVATE TSRMLS_CC);}

//内核中提供了定义类属性的宏ZEND_API int zend_declare_property(zend_class_entry *ce, 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, char *doc_comment, int doc_comment_len TSRMLS_DC);ZEND_API int zend_declare_property_null(zend_class_entry *ce, char *name, int name_length, int access_type TSRMLS_DC);ZEND_API int zend_declare_property_bool(zend_class_entry *ce, char *name, int name_length, long value, int access_type TSRMLS_DC);ZEND_API int zend_declare_property_long(zend_class_entry *ce, char *name, int name_length, long value, int access_type TSRMLS_DC);ZEND_API int zend_declare_property_double(zend_class_entry *ce, char *name, int name_length, double value, int access_type TSRMLS_DC);ZEND_API int zend_declare_property_string(zend_class_entry *ce, char *name, int name_length, char *value, int access_type TSRMLS_DC);ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, char *name, int name_length, char *value, int value_len, int access_type TSRMLS_DC);


三、创建类中的方法

#define ZEND_ACC_STATIC                     0x01     /* 静态方法 */#define ZEND_ACC_ABSTRACT                   0x02     /* 抽象方法 */#define ZEND_ACC_FINAL                      0x04     /* 终态方法 */#define ZEND_ACC_IMPLEMENTED_ABSTRACT       0x08     /* fn_flags */#define ZEND_ACC_INTERACTIVE                0x10     /* fn_flags */#define ZEND_ACC_PUBLIC                     0x100    /* PUBLIC */#define ZEND_ACC_PROTECTED                  0x200    /* PROTECTED */#define ZEND_ACC_PRIVATE                    0x400    /* PRIVATE */#define ZEND_ACC_CHANGED                    0x800    /* fn_flags, zend_property_info.flags */#define ZEND_ACC_CTOR                       0x2000   /* __construct */#define ZEND_ACC_DTOR                       0x4000   /* __destruct */#define ZEND_ACC_CLONE                      0x8000   /* __clone */#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 */
在实现类中方法前,先了解一下zend_function_entry。
在新建的扩展中,有一个zend_function_entry,内核通过它把PHP语言中的函数和C语言编写的函数连接,zend_function_entry相当于桥梁。
class中,同样用它来连接PHP中类的方法与C语言编写的方法。

<?phpclass myclass{public function __construct(){echo "init myclass!";}public function hello(){echo "hello";}}?>

内核中:

zend_class_entry *myclass;PHP_METHOD(myclass, __construct){php_printf("init myclass!");}PHP_METHOD(myclass, hello){php_printf("hello");}static zend_function_entry myclass_method[] = {ZEND_ME(myclass, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR);ZEND_ME(myclass, hello, NULL, ZEND_ACC_PUBLIC);{NULL, NULL, NULL}}PHP_MINIT_FUNCTION(extension_name){zend_class_entry class;INIT_CLASS_ENTRY(class, "myclass", myclass_method);myclass = zend_register_internal_class(&class TSRMLS_CC);return SUCCESS;}



四、创建类中的常量

使用内核提供的API即可。

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);
<?phpclass myclass{const class_name = "myclass";}?>
内核中:
zend_class_entry *myclass;static zend_function_entry myclass_method[] = {{NULL, NULL, NULL}}PHP_MINIT_FUNCTION(extension_name){zend_class_entry class;INIT_CLASS_ENTRY(class, "myclass", myclass_method);myclass = zend_register_internal_class(&class TSRMLS_CC);zend_declare_class_constant_stringl(myclass, "class_name", strlen("class_name"), "myclass", strlen("myclass") TSRMLS_CC);}