Usage Analysis of _ initialize () and Class constructor _ construct () in ThinkPHP

Source: Internet
Author: User
This article mainly introduces the usage of _ initialize () and construct _ construct () in ThinkPHP, and analyzes the method of constructing subclass during class initialization in ThinkPHP as an example, is oriented using ThinkPHP

This article mainly introduces the usage of _ initialize () and construct _ construct () in ThinkPHP, and analyzes the method of constructing subclass during class initialization in ThinkPHP as an example, is oriented using ThinkPHP

This article analyzes _ initialize () and constructor _ construct () in ThinkPHP (). Share it with you for your reference. The specific analysis is as follows:

_ 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:

The Code is as follows:

Class Action {
Public function _ construct ()
{
Echo 'Hello action ';
}
}
Class IndexAction extends Action {
Public function _ construct ()
{
Echo 'Hello indexaction ';
}
}
$ Test = new IndexAction;
// 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:

The Code is as follows:

Class IndexAction 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:

Class IndexAction extends Action {
Public function _ construct ()
{
Parent: :__ construct ();
Echo 'Hello indexaction ';
}
}


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.

The Code is as follows:

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


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.

I hope this article will help you with ThinkPHP framework programming.

,

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.