Differences between initialize and construct in ThinkPHP, thinkphpinitialize

Source: Internet
Author: User

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.

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.