|
发表于 2013-4-12 14:44:20
|
|阅读模式
来自 中国广东广州
本帖最后由 萧过无痕 于 2013-4-12 20:11 编辑
首先,感谢你的阅读,本人观点:学习编程是个漫长的过程并不是一时就会很厉害的,关键是坚持,快乐地学习,在这里分享我的一些笔记给你;
众所周知,由于历史原因,PHP之前是使用类名作为构造函数,在PHP 5中引入的新的构造函数__construct。为了实现向后兼容性,如果PHP 5在类中找不到 __construct() 函数,它就会尝试寻找旧式的构造函数,也就是和类同名的函数。
因此唯一会产生兼容性问题的情况是:类中已有一个名为 __construct() 的方法,但它却又不是构造函数。有如下一段代码:
<?php class Foo { public function Foo() { } private function __construct() { } } new Foo(); die(); 此时,输出为:
Fatal error: Call to private Foo::__construct() from invalid context
此时,PHP识别出来的构造函数是__construct,因为是private,于是在外部调用出错。好吧,我们从PHP的C源码中查找一下原因吧。从SQL的扩展类中直接查找类的定义开始:
spl_iterators.c 3228行 REGISTER_SPL_STD_CLASS_EX(IteratorIterator, spl_dual_it_new, spl_funcs_IteratorIterator); ///spl_functions.h 31行 #define REGISTER_SPL_STD_CLASS_EX(class_name, obj_ctor, funcs) \ spl_register_std_class(&spl_ce_ ## class_name, # class_name, obj_ctor, funcs TSRMLS_CC); //spl_functions.c 41行 PHPAPI void spl_register_std_class(zend_class_entry ** ppce, char * class_name, void * obj_ctor, const zend_function_entry * function_list TSRMLS_DC) //spl_functions.c 2235行 ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry TSRMLS_DC) /* {{{ */ //调用do_register_internal_class函数 //zend_API.c 2169行 static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, zend_uint ce_flags TSRMLS_DC) /* {{{ */ //调用 zend_register_functions(class_entry, class_entry->builtin_functions, &class_entry->function_table, MODULE_PERSISTENT TSRMLS_CC); //zend_API.c 1795行 /* Look for ctor, dtor, clone * If it’s an old-style constructor, store it only if we don’t have * a constructor already. */ if ((fname_len == class_name_len) && !memcmp(lowercase_name, lc_class_name, class_name_len+1) && !ctor) { ctor = reg_function; } else if ((fname_len == sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME))) { ctor = reg_function; } scope->constructor = ctor; //在1961行 确认构造函数 以上代码为PHP 5.3.0版本
从以上跟踪流程来看,程序在注册所有函数时,如果存在__construct(即ZEND_CONSTRUCTOR_FUNC_NAME)时,会覆盖class_name(类名)的构造函数,使其作为常规的成员函数存在。如下所示代码:
<?php class Foo { public function Foo() { echo ’Foo’; } public function __construct() { echo ’__construct’; } } $foo = new Foo(); $foo->Foo(); 对于在前面的示例中的报错,我们可以在zend/zend_object_handlers.c 1057行ZEND_API union _zend_function *zend_std_get_constructor(zval *object TSRMLS_DC)找到出处。
|
|