设为首页 收藏本站
查看: 649|回复: 0

[经验分享] PHP源码分析-面向对象(一)

[复制链接]

尚未签到

发表于 2017-3-31 09:09:44 | 显示全部楼层 |阅读模式
在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中的代码:
<?php
class 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_*系列函数,同时需要三个信息:

属性名称、属性默认值、属性访问权限。

<?php
class 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语言编写的方法。

<?php
class 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);
<?php
class 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);
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-357921-1-1.html 上篇帖子: Objective-C和PHP进行json交互 下篇帖子: 推荐一个好的PHP Framework –Yii Framework
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表