Usage analysis of __initialize() and class constructor __construct() in ThinkPHP, thinkphp constructor_PHP tutorial

WBOY
Release: 2016-07-13 10:12:38
Original
1304 people have browsed it

Usage analysis of __initialize() and class constructor __construct() in ThinkPHP, thinkphp constructor

This article analyzes __initialize() and the class constructor __construct() in ThinkPHP through examples. Share it with everyone for your reference. The specific analysis is as follows:

__construct in thinkphp cannot be used casually, because your module class inherits the superior class, and the superior class is defined;

1. __initialize() is not a function in the php class. The only constructor of the php class is __construct().

2. Class initialization: If the subclass has its own constructor (__construct()), it will call its own to initialize. If not, it will call the parent class's constructor to initialize itself.

3. When both the subclass and the parent class have __construct() functions, if you want to call the parent class's __constrcut() at the same time when initializing the subclass, you can use parent::__construct in the subclass. ().

If we write two classes, as follows:

Copy code 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, so the output is 'hello IndexAction', but change the subclass to:
Copy code The code is as follows:
class IndexAction extends Action{
Public function __initialize()

echo 'hello IndexAction';
}  
}

Then 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?
Copy code 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 way is to call the method of the subclass in the parent class.
Copy code 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.

So, the appearance of __initialize() in ThinkPHP is just to facilitate programmers to avoid frequent use of parent::__construct() when writing subclasses, and at the same time correctly call the constructor of the parent class in the framework, so, we When initializing a subclass in ThnikPHP, use __initialize() instead of __construct(). Of course, you can also modify the __initialize() function to your favorite function name by modifying the framework.

I hope this article will be helpful to everyone’s ThinkPHP framework programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/919629.htmlTechArticle Usage analysis of __initialize() and class constructor __construct() in ThinkPHP, example analysis of thinkphp constructor in this article __initialize() and class constructor __construct() in ThinkPHP. Points...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!