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

来源:互联网 发布:乐乎公寓的房子怎么样 编辑:程序博客网 时间:2024/06/06 04:30
在PHP中,面向对象与Class是密切相关的,来看看在内核中如何实现Class的接口、继承、私有、受保护、公开等特性。


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

PHP中看起来像这样:

[php] view plaincopy
  1. <?php  
  2.     $obj = new myclass();  
  3. ?>  

内核中:
[cpp] view plaincopy
  1. zend_class_entry *myclass;  //定义全局指针变量,指向myclass类。  
  2.   
  3. static zend_function_entry myclass_method[] = { //myclass中的方法  
  4.     {NULL, NULL, NULL}  
  5. }  
  6.   
  7. PHP_MINIT_FUNCTION(extension_name){  
  8.     zend_class_entry class;  
  9.     INIT_CLASS_ENTRY(class"myclass", myclass_method);  
  10.     myclass = zend_register_internal_class(&class TSRMLS_CC);  
  11.     return SUCCESS;  
  12. }  

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


对应上述myclass类的实现,在PHP中的代码:
[php] view plaincopy
  1. <?php  
  2. class myclass{  
  3. }  
  4. ?>  

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


二、创建类的属性

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

[cpp] view plaincopy
  1. #define ZEND_ACC_STATIC                     0x01     /* Static */  
  2. #define ZEND_ACC_PUBLIC                     0x100    /* Public */  
  3. #define ZEND_ACC_PROTECTED                  0x200    /* Protected */  
  4. #define ZEND_ACC_PRIVATE                    0x400    /* Private */  

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

[php] view plaincopy
  1. <?php  
  2. class myclass{  
  3.     private my_var="this is property";  
  4. }  
  5. ?>  

内核中:
[cpp] view plaincopy
  1. zend_class_entry *myclass;  
  2. static zend_function_entry myclass_method[] = {  
  3.     {NULL, NULL, NULL}  
  4. }  
  5.   
  6. PHP_MINIT_FUNCTION(extension_name){  
  7.     zend_class_entry class;  
  8.     INIT_CLASS_ENTRY(class"myclass", myclass_method);  
  9.     myclass = zend_register_internal_class(&class TSRMLS_CC);  
  10.     zend_declare_property_string(myclass, "my_var", strlen("my_var"), "this is property", ZEND_ACC_PRIVATE TSRMLS_CC);  
  11. }  

[cpp] view plaincopy
  1. //内核中提供了定义类属性的宏  
  2. ZEND_API int zend_declare_property(zend_class_entry *ce, char *name, int name_length, zval *property, int access_type TSRMLS_DC);  
  3. 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);  
  4. ZEND_API int zend_declare_property_null(zend_class_entry *ce, char *name, int name_length, int access_type TSRMLS_DC);  
  5. ZEND_API int zend_declare_property_bool(zend_class_entry *ce, char *name, int name_length, long value, int access_type TSRMLS_DC);  
  6. ZEND_API int zend_declare_property_long(zend_class_entry *ce, char *name, int name_length, long value, int access_type TSRMLS_DC);  
  7. ZEND_API int zend_declare_property_double(zend_class_entry *ce, char *name, int name_length, double value, int access_type TSRMLS_DC);  
  8. ZEND_API int zend_declare_property_string(zend_class_entry *ce, char *name, int name_length, char *value, int access_type TSRMLS_DC);  
  9. 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);  


三、创建类中的方法

[cpp] view plaincopy
  1. #define ZEND_ACC_STATIC                     0x01     /* 静态方法 */  
  2. #define ZEND_ACC_ABSTRACT                   0x02     /* 抽象方法 */  
  3. #define ZEND_ACC_FINAL                      0x04     /* 终态方法 */  
  4. #define ZEND_ACC_IMPLEMENTED_ABSTRACT       0x08     /* fn_flags */  
  5. #define ZEND_ACC_INTERACTIVE                0x10     /* fn_flags */  
  6. #define ZEND_ACC_PUBLIC                     0x100    /* PUBLIC */  
  7. #define ZEND_ACC_PROTECTED                  0x200    /* PROTECTED */  
  8. #define ZEND_ACC_PRIVATE                    0x400    /* PRIVATE */  
  9. #define ZEND_ACC_CHANGED                    0x800    /* fn_flags, zend_property_info.flags */  
  10. #define ZEND_ACC_CTOR                       0x2000   /* __construct */  
  11. #define ZEND_ACC_DTOR                       0x4000   /* __destruct */  
  12. #define ZEND_ACC_CLONE                      0x8000   /* __clone */  
  13. #define ZEND_ACC_ALLOW_STATIC               0x10000  /* fn_flags */  
  14. #define ZEND_ACC_SHADOW                     0x20000  /* fn_flags */  
  15. #define ZEND_ACC_DEPRECATED                 0x40000  /* fn_flags */  
  16. #define ZEND_ACC_CLOSURE                    0x100000 /* fn_flags */  
  17. #define ZEND_ACC_CALL_VIA_HANDLER           0x200000 /* fn_flags */  
在实现类中方法前,先了解一下zend_function_entry。
在新建的扩展中,有一个zend_function_entry,内核通过它把PHP语言中的函数和C语言编写的函数连接,zend_function_entry相当于桥梁。
class中,同样用它来连接PHP中类的方法与C语言编写的方法。

[php] view plaincopy
  1. <?php  
  2. class myclass{  
  3.     public function __construct(){  
  4.         echo "init myclass!";  
  5.     }  
  6.   
  7.     public function hello(){  
  8.         echo "hello";  
  9.     }  
  10. }  
  11. ?>  

内核中:

[cpp] view plaincopy
  1. zend_class_entry *myclass;  
  2.   
  3. PHP_METHOD(myclass, __construct){  
  4.     php_printf("init myclass!");  
  5. }  
  6.   
  7. PHP_METHOD(myclass, hello){  
  8.     php_printf("hello");  
  9. }  
  10.   
  11. static zend_function_entry myclass_method[] = {  
  12.     ZEND_ME(myclass, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR);  
  13.     ZEND_ME(myclass, hello, NULL, ZEND_ACC_PUBLIC);  
  14.     {NULL, NULL, NULL}  
  15. }  
  16.   
  17. PHP_MINIT_FUNCTION(extension_name){  
  18.     zend_class_entry class;  
  19.     INIT_CLASS_ENTRY(class"myclass", myclass_method);  
  20.       
  21.     myclass = zend_register_internal_class(&class TSRMLS_CC);  
  22.     return SUCCESS;  
  23. }  



四、创建类中的常量

使用内核提供的API即可。

[cpp] view plaincopy
  1. ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value TSRMLS_DC);  
  2. ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length TSRMLS_DC);  
  3. ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, long value TSRMLS_DC);  
  4. ZEND_API int zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value TSRMLS_DC);  
  5. ZEND_API int zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value TSRMLS_DC);  
  6. 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);  
  7. ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value TSRMLS_DC);  
[php] view plaincopy
  1. <?php  
  2. class myclass{  
  3.     const class_name = "myclass";  
  4. }  
  5. ?>  
内核中:
[cpp] view plaincopy
  1. zend_class_entry *myclass;  
  2. static zend_function_entry myclass_method[] = {  
  3.     {NULL, NULL, NULL}  
  4. }  
  5.   
  6. PHP_MINIT_FUNCTION(extension_name){  
  7.     zend_class_entry class;  
  8.     INIT_CLASS_ENTRY(class"myclass", myclass_method);  
  9.     myclass = zend_register_internal_class(&class TSRMLS_CC);  
  10.       
  11.     zend_declare_class_constant_stringl(myclass, "class_name", strlen("class_name"), "myclass", strlen("myclass") TSRMLS_CC);  
  12. }  
0 0
原创粉丝点击