The recognition of PHP construction method in object-oriented

Source: Internet
Author: User

It is well known that, for historical reasons, PHP was preceded by a new constructor, introduced in PHP 5, using class masterpieces as a constructor __construct. For backward compatibility, if PHP 5 does not find the __construct () function in the class, it tries to look for a legacy constructor, which is a function with the same name as the class.

So the only thing that will create a compatibility problem is that there is already a method named __construct () in the class, but it is not a constructor. The following code is available:

 
  
  1. <?php
  2. Class Foo {
  3. Public Function Foo () {
  4. }
  5. Private Function __construct () {
  6. }
  7. }
  8. New Foo ();
  9. Die ();

At this point, the output is:

Fatal Error:call to Private foo::__construct () from invalid context

At this point, PHP recognized the constructor is __construct, because it is private, so the external call error. OK, let's look for the reason from the C source of PHP. Start by finding the definition of a class directly from an extended class in sql:

 
  1. SPL_ITERATORS.C 3228 rows of register_spl_std_class_ex (Iteratoriterator, Spl_dual_it_new, spl_funcs_iteratoriterator);
  2. Spl_functions.h 31 Lines
  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 Lines
  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 Lines
  8. Zend_api zend_class_entry *zend_register_internal_class (zend_class_entry *orig_class_entry TSRMLS_DC)/* {{* * *
  9. Calling the Do_register_internal_class function
  10. ZEND_API.C 2169 Lines
  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 1795 Lines
  15. /* Look for ctor, Dtor, clone
  16. * If it's a 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; Confirm the constructor on line 1961

The above code is PHP 5.3.0 version

From the above tracing process, when a program registers all functions, if there is a __construct (that is, zend_constructor_func_name), it overwrites the constructor of the Class_name (class name) to make it exist as a regular member function. As shown in the following code:

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

For the error in the previous example, we can ZEND_API union _zend_function *zend_std_get_constructor (Zval * in ZEND/ZEND_OBJECT_HANDLERS.C 1057 lines). Object TSRMLS_DC) to find the source.




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.