下面由thinkphp教程欄目給大家介紹ThinkPHP5之 _initialize() 初始化方法,希望對(duì)需要的朋友有所幫助!
ThinkPHP5之 _initialize() 初始化方法詳解
前言
_initialize()
這個(gè)方法在官方手冊(cè)里是這樣說的:
如果你的控制器類繼承了thinkController類的話,可以定義控制器初始化方法_initialize,在該控制器的方法調(diào)用之前首先執(zhí)行。
其實(shí)不止5,在之前的版本中也出現(xiàn)過,這里和大家聊一聊它的實(shí)現(xiàn)過程吧。
示例
下面是官方手冊(cè)上給的示例:
namespace appindexcontroller; use thinkController; class Index extends Controller { public function _initialize() { echo 'init<br/>'; } public function hello() { return 'hello'; } public function data() { return 'data'; } }
如果訪問
http://localhost/index.php/index/Index/hello
會(huì)輸出
init hello
如果訪問
http://localhost/index.php/index/Index/data
會(huì)輸出
init data
分析
因?yàn)槭褂帽仨氁^承thinkController
類,加上這個(gè)又是初始化,所以我們首先就想到了thinkController
類中的 __construct()
,一起來看代碼:
/** * 架構(gòu)函數(shù) * @param Request $request Request對(duì)象 * @access public */ public function __construct(Request $request = null) { if (is_null($request)) { $request = Request::instance(); } $this->view = View::instance(Config::get('template'), Config::get('view_replace_str')); $this->request = $request; // 控制器初始化 if (method_exists($this, '_initialize')) { $this->_initialize(); } // 前置操作方法 if ($this->beforeActionList) { foreach ($this->beforeActionList as $method => $options) { is_numeric($method) ? $this->beforeAction($options) : $this->beforeAction($method, $options); } } }
細(xì)心的你一定注意到了,在整個(gè)構(gòu)造函數(shù)中,有一個(gè)控制器初始化的注釋,而下面代碼就是實(shí)現(xiàn)這個(gè)初始化的關(guān)鍵:
// 控制器初始化 if (method_exists($this, '_initialize')) { $this->_initialize(); }
真相出現(xiàn)了有木有?!
其實(shí)就是當(dāng)子類繼承父類后,在沒有重寫構(gòu)造函數(shù)的情況下,也自然繼承了父類的構(gòu)造函數(shù),相應(yīng)的,進(jìn)行判斷當(dāng)前類中是否存在 _initialize
方法,有的話就執(zhí)行,這就是所謂的控制器初始化
的原理。
延伸
如果子類繼承了父類后,重寫了構(gòu)造方法,注意調(diào)用父類的__construct()
哦,否則是使用不了的,代碼如下:
public function __construct() { parent::__construct(); ...其他代碼... }
總結(jié)
一個(gè)簡(jiǎn)單的小設(shè)計(jì),這里拋磚引玉的分析下,希望對(duì)大家有幫助。
鏈接
相關(guān)手冊(cè)頁面:http://www.kancloud.cn/manual/thinkphp5/118049