Home > PHP Framework > ThinkPHP > Analysis of ThinkPHP5's _initialize() initialization method

Analysis of ThinkPHP5's _initialize() initialization method

藏色散人
Release: 2021-03-18 08:54:21
forward
5096 people have browsed it

The following tutorial column will introduce to you the _initialize() initialization method of ThinkPHP5. I hope it will be helpful to friends in need!

ThinkPHP5's _initialize() Detailed explanation of the initialization methodAnalysis of ThinkPHP5's _initialize() initialization method

Preface

_initialize ()

This method is said in the official manual:

If your controller class inherits the \think\Controller class, you can define the controller initialization method _initialize, in the controller is executed first before the method is called. In fact, there are more than 5, it has also appeared in previous versions. Let’s talk to you about its implementation process.

Example

The following is an example given in the official manual:

namespace app\index\controller;

use think\Controller;

class Index extends Controller
{

    public function _initialize()
    {
        echo 'init<br/>';
    }

    public function hello()
    {
        return 'hello';
    }

    public function data()
    {
        return 'data';
    }
}
Copy after login

If you access

http://localhost/index.php/index/Index/hello
Copy after login

, it will output

init
hello
Copy after login

If you access

http://localhost/index.php/index/Index/data
Copy after login

will output

init
data
Copy after login

Analysis

Because the use must inherit the

\think\Controller

class, plus this is initialization, so we first I thought of

__construct()

in the \think\Controller class. Let’s take a look at the code: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">/**      * 架构函数      * @param Request    $request     Request对象      * @access public      */     public function __construct(Request $request = null)     {         if (is_null($request)) {             $request = Request::instance();         }         $this-&gt;view    = View::instance(Config::get('template'), Config::get('view_replace_str'));         $this-&gt;request = $request;         // 控制器初始化         if (method_exists($this, '_initialize')) {             $this-&gt;_initialize();         }         // 前置操作方法         if ($this-&gt;beforeActionList) {             foreach ($this-&gt;beforeActionList as $method =&gt; $options) {                 is_numeric($method) ?                 $this-&gt;beforeAction($options) :                 $this-&gt;beforeAction($method, $options);             }         }     }</pre><div class="contentsignin">Copy after login</div></div> If you are careful, you must have noticed that in the entire constructor, There is a comment for controller initialization, and the following code is the key to achieving this initialization: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">// 控制器初始化 if (method_exists($this, '_initialize')) {     $this-&gt;_initialize(); }</pre><div class="contentsignin">Copy after login</div></div> Has the truth emerged? !

In fact, when a child class inherits the parent class, it will naturally inherit the parent class's constructor without overriding the constructor. Correspondingly, it will be judged whether there is

_initialize in the current class.

Methods are executed if available. This is the so-called

controller initialization

principle. ExtensionIf the subclass inherits the parent class and rewrites the constructor method, pay attention to calling the parent class's

__construct()

, otherwise it will not be used. The code is as follows:

public function __construct()
{
    parent::__construct();
    ...其他代码...
}
Copy after login

SummaryA simple little design, here is a brief analysis, I hope it will be helpful to everyone.

Link

Related manual page:

http://www.kancloud.cn/manual/thinkphp5/118049

The above is the detailed content of Analysis of ThinkPHP5's _initialize() initialization method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
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