This article mainly introduces the differences between class constructor _ construct () and _ initialize () in ThinkPHP. This article describes in detail and I believe it will be of reference value to everyone, let's take a look. This article mainly introduces the differences between class constructor _ construct () and _ initialize () in ThinkPHP. This article describes in detail and I believe it will be of reference value to everyone, let's take a look.
Preface
I believe that phper familiar with THINKPHP is basically familiar._initialize()This method seems to be rarely used._construct() Unless you write the plug-in yourself, it is rarely used.
Today, I suddenly saw the code._construct()The built-in php constructor is unfamiliar to me. although I used to learn java before, I don't have to forget it for a long time. My usual habit is to write the focus of my knowledge
In my little note, I haven't written for a long time. the elegant and agile writing method I used in high school was completely lost to the different dimensions. In addition to the previous ideas, I learned how to write a blog by the technical experts. this is not an effort, but a result of my work that never fades away with time. Let's take a look at the detailed introduction.
First paste the code (my environment is wamp, using the TP framework ):
Created FatherAction. class. php file
SonAction. class. php file created
Runindex()The output result is as follows:
son
If you change the subclass:
Running result;
fatherson
The above results can draw a conclusion:
The constructor of the parent class is not automatically called when the constructor of the subclass is executed. if you want to call the constructor, addparent::construct()
When we change the above constructorTHINKPHP_initialize()The result is consistent with the previous one. to execute_initialize()Method, you also need to use this sentence:parent::_initialize()
Does it mean php built-in constructor?construct()And THINKPHP_initialize()What about the same method?
First, paste two pieces of code:
When the index method of the subclass SonAction is executed, the output result is father.
That is, the subclass calls the constructor of the parent class, but does not call the constructor of the subclass._initialize()Method
Paste the following two codes:
hello(); } echo 'father'; }}?>
Execute the index method of the subclass SonAction and find that the input result is hellofather.
Therefore, we can draw a conclusion:
When the parent class of THINKPHP has a constructor and the child class is not, THINKPHP will not execute the child class_initialize() ;
When the parent class subclass of THINKPHP has constructor, the constructor that calls the parent class must useparent::construct() -----------------_initialize()Likewise;
If the THINKPHP subclass exists at the same timeconstructConstructor and_initialize()Method, only the sub-classconstructConstructor (I personally tested this, the above code does not exist ).
Summary
The above is a detailed description of the differences between class construct () and _ initialize () in ThinkPHP. For more information, see other related articles in the first PHP community!