__initialize () and class constructors in thinkphp __construct ()

Source: Internet
Author: User
Tags php class vars

__initialize () and class constructors in thinkphp __construct ()
There's a lot of talk and usage on the internet about __initialize (), it's always a little bit wrong, so I tested it myself. Share the results with everyone. No, please correct it.
First of all, what I'm going to say is
1, __initialize () is not a function in the PHP class, the constructor of the PHP class is only __construct ().
2, class initialization: If the subclass has its own constructor (__construct ()), then call its own initialization, if not, call the parent class's constructor to do its own initialization.
3, when both the subclass and the parent class have the __construct () function, if you want to call the parent class's __constrcut () at the same time when the subclass is initialized, you can use Parent::__construct () in the subclass.

If we write two classes, as follows:

[PHP]View Plaincopyprint?
  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


It is obvious that the subclass Indexaction is initialized by invoking its own constructor, so the output is ' Hello Indexaction '.
However, the sub-class is modified to

[PHP]View Plaincopyprint?
    1. Class Indexaction extends action{
    2. Public function __initialize ()
    3. {
    4. echo ' Hello indexaction ';
    5. }
    6. }


Then the output is ' Hello Action '. Because the subclass Indexaction does not have its own constructor.
What if I want to invoke the constructor of the parent class at the same time when I initialize the subclass?

[PHP]View Plaincopyprint?
    1. Class Indexaction extends action{
    2. Public function __construct ()
    3. {
    4. Parent::__construct ();
    5. echo ' Hello indexaction ';
    6. }
    7. }


This allows two sentences to be output simultaneously.
There is, of course, a way to call a subclass in the parent class.

[PHP]View Plaincopyprint?
  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. }


This can also output two sentences at the same time.
The method Hello () in the subclass here is similar to __initialize () in thinkphp.
Therefore, the appearance of __initialize () in thinkphp is only convenient for programmers to avoid frequent use of parent::__construct () when writing subclasses, and to correctly invoke the constructor of the parent class within the framework, so We use __initialize () instead of __construct () when initializing subclasses in thnikphp, and of course you can modify the __initialize () function to your preferred function name by modifying the framework.

__initialize () and class constructors in thinkphp __construct ()

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.