Home >php教程 >php手册 >面向对象中PHP构造方法的识别

面向对象中PHP构造方法的识别

WBOY
WBOYOriginal
2016-06-21 08:55:471048browse

  众所周知,由于历史原因,PHP之前是使用类名作为构造函数,在PHP 5中引入的新的构造函数__construct。为了实现向后兼容性,如果PHP 5在类中找不到 __construct() 函数,它就会尝试寻找旧式的构造函数,也就是和类同名的函数。

  因此唯一会产生兼容性问题的情况是:类中已有一个名为 __construct() 的方法,但它却又不是构造函数。有如下一段代码:

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li>
<span class="keyword">class</span><span> Foo {    </span>
</li>
<li class="alt"><span>     </span></li>
<li>
<span>    </span><span class="keyword">public</span><span> </span><span class="keyword">function</span><span> Foo() {    </span>
</li>
<li class="alt"><span>     </span></li>
<li><span>    }    </span></li>
<li class="alt"><span>     </span></li>
<li>
<span>    </span><span class="keyword">private</span><span> </span><span class="keyword">function</span><span> __construct() {    </span>
</li>
<li class="alt"><span>     </span></li>
<li><span>    }    </span></li>
<li class="alt"><span>}    </span></li>
<li><span>     </span></li>
<li class="alt">
<span class="keyword">new</span><span> Foo();    </span>
</li>
<li>
<span class="keyword">die</span><span>();   </span>
</li>
</ol>

  此时,输出为:

  Fatal error: Call to private Foo::__construct() from invalid context

  此时,PHP识别出来的构造函数是__construct,因为是private,于是在外部调用出错。好吧,我们从PHP的C源码中查找一下原因吧。从SQL的扩展类中直接查找类的定义开始:

<ol class="dp-c">
<li class="alt"><span><span>spl_iterators.c 3228行 REGISTER_SPL_STD_CLASS_EX(IteratorIterator, spl_dual_it_new, spl_funcs_IteratorIterator);    </span></span></li>
<li>
<span class="comment">///spl_functions.h 31行   </span><span> </span>
</li>
<li class="alt"><span>#define REGISTER_SPL_STD_CLASS_EX(class_name, obj_ctor, funcs) \    </span></li>
<li><span> spl_register_std_class(&spl_ce_ ## class_name, # class_name, obj_ctor, funcs TSRMLS_CC);    </span></li>
<li class="alt">
<span class="comment">//spl_functions.c 41行   </span><span> </span>
</li>
<li>
<span>PHPAPI void spl_register_std_class(zend_class_entry ** ppce, char * class_name, void * obj_ctor, </span><span class="keyword">const</span><span> zend_function_entry * function_list TSRMLS_DC)    </span>
</li>
<li class="alt"><span>     </span></li>
<li>
<span class="comment">//spl_functions.c 2235行   </span><span> </span>
</li>
<li class="alt">
<span>ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry TSRMLS_DC) </span><span class="comment">/* {{{ */</span><span>    </span>
</li>
<li>
<span class="comment">//调用do_register_internal_class函数   </span><span> </span>
</li>
<li class="alt"><span>     </span></li>
<li>
<span class="comment">//zend_API.c 2169行   </span><span> </span>
</li>
<li class="alt">
<span class="keyword">static</span><span> zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, zend_uint ce_flags TSRMLS_DC) </span><span class="comment">/* {{{ */</span><span>    </span>
</li>
<li>
<span class="comment">//调用   </span><span> </span>
</li>
<li class="alt"><span>zend_register_functions(class_entry, class_entry->builtin_functions, &class_entry->function_table, MODULE_PERSISTENT TSRMLS_CC);    </span></li>
<li><span>     </span></li>
<li class="alt">
<span class="comment">//zend_API.c 1795行   </span><span> </span>
</li>
<li>
<span class="comment">/* Look for ctor, dtor, clone   </span> </li>
<li class="alt"><span><span class="comment">* If it's an old-style constructor, store it only if we don't have   </span> </span></li>
<li><span><span class="comment">* a constructor already.   </span> </span></li>
<li class="alt"><span><span class="comment">*/</span><span>    </span></span></li>
<li>
<span class="keyword">if</span><span> ((fname_len == class_name_len) && !memcmp(lowercase_name, lc_class_name, class_name_len+1) && !ctor) {    </span>
</li>
<li class="alt"><span> ctor = reg_function;    </span></li>
<li>
<span>} </span><span class="keyword">else</span><span> </span><span class="keyword">if</span><span> ((fname_len == sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME))) {    </span>
</li>
<li class="alt"><span> ctor = reg_function;    </span></li>
<li><span>}     </span></li>
<li class="alt"><span>     </span></li>
<li>
<span>scope->constructor = ctor; </span><span class="comment">//在1961行 确认构造函数 </span><span> </span>
</li>
</ol>

  以上代码为PHP 5.3.0版本

  从以上跟踪流程来看,程序在注册所有函数时,如果存在__construct(即ZEND_CONSTRUCTOR_FUNC_NAME)时,会覆盖class_name(类名)的构造函数,使其作为常规的成员函数存在。如下所示代码:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span class="tag-name">php</span><span>   </span></span></li>
<li><span>class Foo {    </span></li>
<li class="alt"><span>     </span></li>
<li><span>    public function Foo() {    </span></li>
<li class="alt"><span>        echo 'Foo';    </span></li>
<li><span>    }    </span></li>
<li class="alt"><span>     </span></li>
<li><span>    public function __construct() {    </span></li>
<li class="alt"><span>        echo '__construct';    </span></li>
<li><span>    }    </span></li>
<li class="alt"><span>}    </span></li>
<li><span>     </span></li>
<li class="alt">
<span>$</span><span class="attribute">foo</span><span> = </span><span class="attribute-value">new</span><span> Foo();    </span>
</li>
<li>
<span>$foo-</span><span class="tag">></span><span>Foo();  </span>
</li>
</ol>

  对于在前面的示例中的报错,我们可以在zend/zend_object_handlers.c 1057行ZEND_API union _zend_function *zend_std_get_constructor(zval *object TSRMLS_DC)找到出处。




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn