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:
- <?php
- Class Foo {
- Public Function Foo () {
- }
- Private Function __construct () {
- }
- }
- New Foo ();
- 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:
- SPL_ITERATORS.C 3228 rows of register_spl_std_class_ex (Iteratoriterator, Spl_dual_it_new, spl_funcs_iteratoriterator);
- Spl_functions.h 31 Lines
- #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 Lines
- 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 Lines
- Zend_api zend_class_entry *zend_register_internal_class (zend_class_entry *orig_class_entry TSRMLS_DC)/* {{* * *
- Calling the Do_register_internal_class function
- ZEND_API.C 2169 Lines
- Static Zend_class_entry *do_register_internal_class (zend_class_entry *orig_class_entry zend_uint ce_flags TSRMLS_DC ) /* {{{ */
- Call
- Zend_register_functions (Class_entry, class_entry->builtin_functions, &class_entry->function_table, Module_persistent tsrmls_cc);
- ZEND_API.C 1795 Lines
- /* Look for ctor, Dtor, clone
- * If it's a 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; 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:
- <?php
- class foo {
-
- public function foo () {
- echo ' Foo ';
- }
- & nbsp.
- public function __construct () {
- echo ' __construct ';
- }
- }
- &NBSP;&NB sp;
-
- $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.