因此唯一会产生兼容性问题的情况是:类中已有一个名为 __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)找到出处。