自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解PHP對構(gòu)造方法的識別

開發(fā) 后端
眾所周知,由于歷史原因,PHP之前是使用類名作為構(gòu)造函數(shù),在PHP 5中引入的新的構(gòu)造函數(shù)__construct,文章將介紹PHP對構(gòu)造方法的識別。

眾所周知,由于歷史原因,PHP之前是使用類名作為構(gòu)造函數(shù),在PHP 5中引入的新的構(gòu)造函數(shù)__construct。為了實(shí)現(xiàn)向后兼容性,如果PHP 5在類中找不到 __construct() 函數(shù),它就會嘗試尋找舊式的構(gòu)造函數(shù),也就是和類同名的函數(shù)。

因此唯一會產(chǎn)生兼容性問題的情況是:類中已有一個(gè)名為 __construct() 的方法,但它卻又不是構(gòu)造函數(shù)。有如下一段代碼:

  1. <?php 
  2. class Foo {  
  3.    
  4.     public function Foo() {  
  5.    
  6.     }  
  7.    
  8.     private function __construct() {  
  9.    
  10.     }  
  11. }  
  12.    
  13. new Foo();  
  14. die();  

此時(shí),輸出為:

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

此時(shí),PHP識別出來的構(gòu)造函數(shù)是__construct,因?yàn)槭莗rivate,于是在外部調(diào)用出錯(cuò)。好吧,我們從PHP的C源碼中查找一下原因吧。從SQL的擴(kuò)展類中直接查找類的定義開始:

  1. spl_iterators.c 3228行 REGISTER_SPL_STD_CLASS_EX(IteratorIterator, spl_dual_it_new, spl_funcs_IteratorIterator);  
  2. ///spl_functions.h 31行  
  3. #define REGISTER_SPL_STD_CLASS_EX(class_name, obj_ctor, funcs) \  
  4.  spl_register_std_class(&spl_ce_ ## class_name, # class_name, obj_ctor, funcs TSRMLS_CC);  
  5. //spl_functions.c 41行  
  6. PHPAPI void spl_register_std_class(zend_class_entry ** ppce, char * class_name, void * obj_ctor, const zend_function_entry * function_list TSRMLS_DC)  
  7.    
  8. //spl_functions.c 2235行  
  9. ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry TSRMLS_DC) /* {{{ */  
  10. //調(diào)用do_register_internal_class函數(shù)  
  11.    
  12. //zend_API.c 2169行  
  13. static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, zend_uint ce_flags TSRMLS_DC) /* {{{ */  
  14. //調(diào)用  
  15. zend_register_functions(class_entry, class_entry->builtin_functions, &class_entry->function_table, MODULE_PERSISTENT TSRMLS_CC);  
  16.    
  17. //zend_API.c 1795行  
  18. /* Look for ctor, dtor, clone  
  19. * If it's an old-style constructor, store it only if we don't have  
  20. * a constructor already.  
  21. */  
  22. if ((fname_len == class_name_len) && !memcmp(lowercase_name, lc_class_name, class_name_len+1) && !ctor) {  
  23.  ctor = reg_function;  
  24. } else if ((fname_len == sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1) && !memcmp(lowercase_name, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME))) {  
  25.  ctor = reg_function;  
  26. }   
  27.    
  28. scope->constructor = ctor; //在1961行 確認(rèn)構(gòu)造函數(shù)  

 

以上代碼為PHP 5.3.0版本

從以上跟蹤流程來看,程序在注冊所有函數(shù)時(shí),如果存在__construct(即ZEND_CONSTRUCTOR_FUNC_NAME)時(shí),會覆蓋class_name(類名)的構(gòu)造函數(shù),使其作為常規(guī)的成員函數(shù)存在。如下所示代碼:

  1. <?php 
  2. class Foo {  
  3.    
  4.     public function Foo() {  
  5.         echo 'Foo';  
  6.     }  
  7.    
  8.     public function __construct() {  
  9.         echo '__construct';  
  10.     }  
  11. }  
  12.    
  13. $foo = new Foo();  
  14. $foo->Foo(); 

對于在前面的示例中的報(bào)錯(cuò),我們可以在zend/zend_object_handlers.c 1057行ZEND_API union _zend_function *zend_std_get_constructor(zval *object TSRMLS_DC)找到出處。

【編輯推薦】

  1. PHP 5魔術(shù)方法應(yīng)用淺析
  2. 改善PHP開發(fā)方式的5種方法
  3. ***PHP網(wǎng)站架構(gòu)工具箱 
責(zé)任編輯:王曉東 來源: PHPPAN
相關(guān)推薦

2009-12-02 16:04:44

PHP fsockop

2009-11-16 16:54:00

PHP構(gòu)造函數(shù)

2009-11-30 17:43:54

PHP split()

2009-09-09 11:24:46

PHP實(shí)現(xiàn)MVC

2009-03-04 13:10:41

SQL語句INSERTDELETE

2009-12-03 18:23:23

2009-11-17 09:02:36

PHP數(shù)組賦值

2009-11-23 14:44:22

PHP 5.0構(gòu)造函數(shù)

2011-05-16 10:35:02

jQuery

2016-10-27 15:58:35

魔術(shù)方法PHP

2009-11-10 10:32:42

2009-11-23 14:30:07

PHP5.0對象模型

2009-12-04 10:19:11

PHP hack

2009-11-18 12:31:19

PHP構(gòu)造函數(shù)

2020-10-24 14:10:26

PHP網(wǎng)絡(luò)安全人機(jī)識別

2016-12-01 19:51:03

PHP魔術(shù)詳解

2009-11-16 16:59:03

PHP構(gòu)造函數(shù)

2009-12-10 13:37:16

PHP parent

2017-07-10 09:21:17

Oracle構(gòu)造序列

2011-08-17 10:58:59

Objective-C構(gòu)造函數(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號