Explain _initialize and __construct from ThinkPHP source code construct 2 tutorial construct 2 crack Chinese version failed to initializ

WBOY
Release: 2016-07-29 08:54:28
Original
1673 people have browsed it

I’ve been studying the ThinkPHP framework recently, and I saw the _initialize() function, so I just made a brief summary

I briefly looked at the tutorials on Google and Baidu, and I felt that there was a lot of talk, and they were all being tested, but I didn’t get to the point. ~

Experimental version: ThinkPHP 3.2.3, PHP5.6. The

_initialize() function appears so that we can call the constructors of the parent class and the subclass at the same time in the subclass.

The easiest way to figure it out is to open the source code of TP, which is the most reliable.

Path: ThinkPHP3.2.3/ThinkPHP/Library/Think/Controller.class.php. (△Controller is an abstract class△)

We can see:
Explain _initialize and __construct from ThinkPHP source code construct 2 tutorial construct 2 crack Chinese version failed to initializ

Here the abstract class Controller is rewritten __construct() method, focus on the three red lines, here is all _initialize(), in fact, there is no special declaration of an _initialize() method and then give it special functions.

It can be seen that _initialize() has the function of a constructor purely because it happens to be in __construct() (when a class is instantiated, the constructor __construct is run. If the current class has an _initialize() method, by the way Executed _initialize())

Let’s take a look at how to complete the requirement of simultaneously calling the constructor of the subclass and parent class in native PHP.
Explain _initialize and __construct from ThinkPHP source code construct 2 tutorial construct 2 crack Chinese version failed to initializ

What about in ThinkPHP?
Explain _initialize and __construct from ThinkPHP source code construct 2 tutorial construct 2 crack Chinese version failed to initializ

If the native code wants to complete the requirements, it needs to call and run parent::__construct() in the subclass.

In ThinkPHP, _initialize() itself can complete this function after the parent class has been specially processed. In fact, I think ThinkPHP's _initialize function is intended to be used when the parent class and child class constructors need to be called at the same time. .

To summarize the precautions for using _initialize() and __construct() in TP

  • If _initialize() and __construct() appear at the same time, then _initialize() will be invalid, because at this time __construct() has been rewritten and no longer calls _initialize().

  • If you want the constructors of the parent and child classes to be called at the same time, you must handle it in the __construct() of the parent class:

    <code>if(method_exists($this,’_initialize’)){
    
    $this -> _initialize();
    
    }
    </code>
    Copy after login
  • Both cannot be overridden and rewritten by the child class, otherwise the functions of the parent and child classes will be called at the same time. It will be invalid

For now (ThinkPHP3.2.3), _initialize() is vulnerable. After all, we still need to do it manually. In this case, the only role of _initialize() is naming constraints. , because you can change _initialize to another name if you like. I believe the official will improve this function in later versions.

The above introduces the explanation of _initialize and __construct from the ThinkPHP source code, including the content of initialize and construct. I hope it will be helpful to friends who are interested in PHP tutorials.

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!