ThinkPHP _ initialize () and construct _ construct () analysis of classes

Source: Internet
Author: User
_ Initialize () and constructor _ construct () in ThinkPHP cannot be used at will, because your module class inherits the upper class, the upper-level class has a defined value. 1. _ initialize () is not a function in the php class, but a constructor of the php class.

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

_ Construct in thinkphp cannot be used at will, because your module class inherits the upper class and the upper class has a defined level;

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

2. class initialization: if a subclass has its own constructor (_ construct (), it calls its own constructor for 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:

  1. Class Action {
  2. Public function _ construct ()
  3. {
  4. Echo 'hello action ';
  5. }
  6. }
  7. Class IndexAction extends Action {
  8. Public function _ construct ()
  9. {
  10. Echo 'hello indexaction ';
  11. }
  12. }
  13. $ Test = new IndexAction;
  14. // Output --- hello IndexAction

Obviously, when initializing the subclass IndexAction, it will call its own constructor. Therefore, the output is 'Hello indexaction', but the subclass is changed:

  1. Class IndexAction extends Action {
  2. Public function _ initialize ()
  3. {
  4. Echo 'hello indexaction ';
  5. }
  6. }

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?

  1. Class IndexAction extends Action {
  2. Public function _ construct ()
  3. {
  4. Parent: :__ construct ();
  5. Echo 'hello indexaction ';
  6. }
  7. }

In this way, two sentences can be output at the same time. of course, another method is to call the subclass method in the parent class.

  1. Class Action {
  2. Public function _ construct ()
  3. {
  4. If (method_exists ($ this, 'Hello '))
  5. {
  6. $ This-> hello ();
  7. }
  8. Echo 'hello action ';
  9. }
  10. }
  11. Class IndexAction extends Action {
  12. Public function hello ()
  13. {
  14. Echo 'hello indexaction ';
  15. }
  16. }

In this way, two sentences can be output at the same time, and the method hello () in the subclass here 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 name 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.