Recognition of PHP constructor in object-oriented

Source: Internet
Author: User
As we all know, due to historical reasons, PHP previously used class names as constructors and introduced the new construct _ construct in PHP5. To implement backward compatibility, if PHP5 cannot find the _ construct () function in the class, it will try to find the old constructor, that is, a function with the same name as the class. Therefore, the only situation that causes compatibility problems "> <LINKhref =" http :/

As we all know, due to historical reasons, PHP previously used class names as constructors and introduced the new construct _ construct in PHP 5. To achieve backward compatibility, if PHP 5 cannot find the _ construct () function in the class, it will try to find the old constructor, that is, the function with the same name as the class.

Therefore, the only situation that causes compatibility problems is that the class already has a method named _ construct (), but it is not a constructor. The following code is provided:

 

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

In this case, the output is:

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

In this case, the constructor identified by PHP is _ construct. because it is private, an error occurs during external calls. Okay. let's look for the reason from the php c source code. Start from the SQL extension class to directly find the class definition:

 

 
 
  1. Spl_iterators.c 3228 row 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. // Spl_functions.c 2235 row
  8. ZEND_API zend_class_entry * zend_register_internal_class (zend_class_entry * orig_class_entry TSRMLS_DC )/*{{{*/
  9. // Call the do_register_internal_class function
  10. // Zend_API.c, row 3
  11. Static zend_class_entry * do_register_internal_class (zend_class_entry * orig_class_entry, zend_uint ce_flags TSRMLS_DC )/*{{{*/
  12. // Call
  13. Zend_register_functions (class_entry, class_entry-> builtin_functions, & class_entry-> function_table, MODULE_PERSISTENT TSRMLS_CC );
  14. // Zend_API.c, row 3
  15. /* Look for ctor, dtor, clone
  16. * If it's an old-style constructor, store it only if we don't have
  17. * A constructor already.
  18. */
  19. If (fname_len = class_name_len )&&! Memcmp (lowercase_name, lc_class_name, class_name_len + 1 )&&! Ctor ){
  20. Ctor = reg_function;
  21. } Else if (fname_len = sizeof (ZEND_CONSTRUCTOR_FUNC_NAME)-1 )&&! Memcmp (lowercase_name, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof (ZEND_CONSTRUCTOR_FUNC_NAME ))){
  22. Ctor = reg_function;
  23. }
  24. Scope-> constructor = ctor; // Check the constructor in row 1961.

The above code is PHP 5.3.0

From the above tracking process, if _ construct (ZEND_CONSTRUCTOR_FUNC_NAME) exists when the program registers all functions, it will overwrite the class_name (class name) constructor, make it exist as a regular member function. The code is as follows:

 

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

For the error in the previous example, we can find the source in zend/zend_object_handlers.c 1057 line ZEND_API union _ zend_function * zend_std_get_constructor (zval * object TSRMLS_DC.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.