The difference between __construct and thinkphp _initialize

Source: Internet
Author: User

At first, I didn't know anything about it, so I thought about it recently.

First of all, I'll talk about inheritance in PHP, __construct is a constructor in a class for instantiating

1<?PHP2     classaction{3          Public function__construct () {4             Echo' Action ';5         }6     }7 8     classSonactionextendsaction{9          Public function__construct () {Ten             Echo' Son Action '; One         } A     } -  -     $son=Newsonaction (); the  -     //Result:son Action -?>

Define constructors in the parent class, use constructors in subclasses, instantiate subclasses, output son Action, do not call constructors in parent class

<?PHPclassaction{ Public function__construct () {Echo' Action '; }    }    classSonactionextendsaction{ Public function__construct () {Parent::__construct (); Echo' Son Action '; }    }    $son=Newsonaction (); //Result:actionson Action?>

In the subclass, the constructor of the parent class is called using Parent::__construct (), because the parent class is inherited, its constructor is defined, the __construct () is overloaded, so the constructor of the parent class needs to be called parent::__construct

<? PHP     class action{        publicfunction  __construct () {            echo ' Action ';        }    }     class extends action{    }    $sonnew  sonaction ();     // result:action?>

There is no own constructor in the inherited subclass, which can be said to inherit the constructor of the parent class (personal understanding, can be explored), so when instantiated, the output gets the result

Next I'm going to talk about the _initialize function.

In fact, this function is not native PHP contains, but a TP own definition of a function

We can see on the frame of TP again that my version is 3.1.3

   /** * Schema function Get Template object instance * @access Public*/     Public function__construct () {tag (' Action_begin ',$this-config); //materialized View Classes        $this->view = think::instance (' View '); //Controller Initialization        if(method_exists($this, ' _initialize '))            $this-_initialize (); }

This function is the constructor in the action, and in the controller initialization, Method_exists is used to determine whether the function _initialize the function in the currently instantiated class.

When a subclass does not have a constructor, the subclass inherits the constructor of the parent class (the subclass inherits [or calls the constructor of the nearest parent class]), and determines whether the subclass contains the _initialize function.

If it contains, call the function, call it at the time of construction, after construct ...

The following example: can be carefully observed

constructor for Action class Public function__construct () {Echo' Grand father '; Tag (' Action_begin ',$this-config); //materialized View Classes        $this->view = think::instance (' View '); //Controller Initialization        if(method_exists($this, ' _initialize '))            $this-_initialize (); } The constructor for the Testaction classclassTestactionextendsaction{ Public function__construct () {Echo' Test override Action '; }} Sonatestaction class initialization functionclassSontestactionextendstestaction{ Public function_initialize () {Echo' Son test '; }                 Public functionindex () {Echo222;  die(); }    }  //output gets test override Action222  

The above example results in an analysis of the result: Testaction overloads the constructor in the action, so when you access index in Sontestaction, the constructor is called first, and the constructor for the parent class is invoked, and the parent class constructor does not contain the call _ Initialize so ... _initialize not called in subclass

Where there are errors can leave a message to discuss.

Reprint please indicate the address reproduced, original poison, reproduced chrysanthemum: http://i.cnblogs.com/EditPosts.aspx?postid=4204198&update=1

The difference between __construct and thinkphp _initialize

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.