_ Initialize () and _ construct () usage in ThinkPHP

Source: Internet
Author: User
Tags constructor php class

1 _ initialize () is not a constructor in the php class. The constructor of the php class only has _ construct ().

Class 2 Initialization: If the subclass has its own construct _ construct (), it calls its own initialization. If it does not,
Then, the constructor of the parent class is called for initialization.

3. When both the subclass and the parent class have the _ construct () function, if you want to call the _ constrcut () function of the parent class at the same time when initializing the child class (),
You can use parent ::__ construct () in the subclass ().

If we write two classes

The code is as follows: Copy code

Class Action {
Public function _ construct (){
Echo 'hello action ';
   }
}   

Class IndexAcion extends Action {
Public function _ construct (){
Echo 'hello, indexaction ';
   }
}

$ Test = new IndexAcion ();
Output --- hello IndexAction

  
Obviously, when initializing the subclass IndexAction, it will call its own constructor, so the output is 'Hello indexaction'
But modify the subclass

The code is as follows: Copy code
Class IndexAcion extends Action {
Public function _ initialize (){
Echo 'hello indexaction ';
   }
}

  
The output is 'Hello action', because the subclass IndexAction does not have its own constructor.
What if I want to call the constructor of the parent class at the same time when initializing the subclass?

The code is as follows: Copy code

Class IndexAcion extends Action {
Public function _ construct (){
Parent: :__ construct ();
Echo 'hello indexaction ';
   }
}
  
In this way, two sentences can be output simultaneously.
Of course, there is another way to call the subclass method in the parent class.

Class Action {
Public function _ construct (){
If (method_exists ($ this, 'Hello ')){
$ This-> hello ();
       }
Echo 'hello action ';
   }
}   

Class IndexAcion extends Action {
Public function hello (){
Echo 'hello indexaction ';
   }
}

  
In this way, two sentences can be output simultaneously.
The method hello () in the subclass is similar to _ initialize () in ThinkPHP ()
Therefore, the emergence of _ initialize () in ThinkPHP only facilitates programmers to avoid frequent use of parent ::__ construct () when writing child classes ()
At the same time, correctly call the constructor of the parent class in the framework. Therefore, we need to use _ initialize () instead of _ construct () when initializing the subclass in ThnikPHP (),
You can also modify the _ initialize () function to your favorite function by modifying the framework.

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.