Differences between initialize and construct in ThinkPHP, thinkphpinitialize
The initialize () and construct () functions in ThinkPHP can be understood as constructor. The previous one is unique to the tp framework, followed by the php constructor, so what are the differences between the two?
In online search, many answers are the same. initialize in ThinkPHP is equivalent to construct in php. This is wrong. If so, why does tp not use construct, what if we need to get a ThinkPHP version of initialize constructor?
Try it and you will know the differences between the two.
a.phpclass a{ function __construct(){ echo 'a'; }}
B. php (note: the constructor does not call parent ::__ construct ();)
include 'a.php';class b extends a{ function __construct(){ echo 'b'; }} $test=new b();
Running result:
B
It can be seen that although class B inherits Class a, the output proves that the program only executes Class B constructor, but does not automatically execute the constructor of the parent class.
If the constructor of B. php is added with parent :__ construct (), it is different.
include 'a.php';class b extends a{ function __construct(){ parent::__construct(); echo 'b'; }} $test=new b();
The output result is:
AB
Then the constructor of the parent class is executed.
Let's take a look at the initialize () function of thinkphp.
BaseAction.class.phpclass BaseAction extends Action{ public function _initialize(){ echo 'baseAction'; } IndexAction.class.phpclass IndexAction extends BaseAction{ public function (){ echo 'indexAction'; }
Run the Index method under index and output the result:
BaseActionindexAcition
It can be seen that the _ initialize method of the subclass automatically calls the _ initialize method of the parent class. Construct of php, if you want to call the method of the parent class, you must call parent :__ construct () in the subclass construct display ();
This is the difference between initialize and construct in ThinkPHP.
In the above discussion, the difference between initialize and construct in ThinkPHP is that all the content I have shared with you is provided. I hope you can give me a reference and support me a lot.