From thinkphp source explanation _initialize and __construct construct 2 tutorial construct 2 hack Chinese version failed to Initializ

Source: Internet
Author: User
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.

  • Related Article

    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.