Recently in the study thinkphp framework, see _initialize () function, also just do a small summary
A simple look at Google, Baidu on the tutorial, feel the crackling said a lot, all in the test, did not mention the idea ~
Experimental version: Thinkphp 3.2.3,php5.6.
The _initialize () function appears so that we can invoke the constructor of the parent and child classes at the same time in the subclass.
To understand the simplest way is to open the source of TP, the most reliable.
Path: thinkphp3.2.3/thinkphp/library/think/controller.class.php. (Controller is an abstract class)
We can see:
Here controller this abstract class overrides the __construct () method, focusing on three red lines, here is _initialize () all, in fact, there is no special declaration of a _initialize () method and then give it special functions.
As you can see, _initialize () has the function of a constructor purely because it is just in __construct () (When the class is instantiated, the constructor __construct runs, and if the current class exists _initialize () method, incidentally executes _ Initialize ())
Let's look at how to do this in native PHP and call the subclass parent constructor requirements.
What about the thinkphp?
The native code wants to complete the requirement by calling run Parent::__construct () in the subclass.
and thinkphp in the parent class after the special processing, _initialize () itself can do this function, in fact, I think thinkphp's _initialize function is intended to be used when the parent class and subclass constructors need to be called at the same time.
Summarize the considerations for using _initialize () and __construct () in TP
If _initialize () and __construct () are present at the same time, then _initialize () will be invalid because __construct () has been rewritten and no longer calls _initialize ().
If you want the parent-child class constructor to be called at the same time, be sure to handle it in the parent class's __construct ():
if(method_exists($this,’_initialize’)){$this -> _initialize();}
Neither can be overridden by a class override, otherwise the function that the parent-child class invokes will be invalidated
For now (ThinkPHP3.2.3), _initialize () is a loophole, after all, we need to do a manual processing, so that _initialize () the role of the rest of the naming constraints, because as long as it is willing to fully can be _ Initialize changed to another name, believing that the official will improve the function in later versions.
The above is introduced from thinkphp source to explain the _initialize and __construct, including the initialize,construct aspects of the content, I hope that the PHP tutorial interested in a friend helpful.