thinkphp登錄限制時__construct和_initialize的區別介紹

thinkphp登錄限制時__construct和_initialize的區別介紹

thinkphp支持兩種構造方法:? __construct和_initialize(ThinkPHP內置的構造方法)。

修改用戶控制器類文件UserController.class.php 如下:

thinkphp登錄限制時__construct和_initialize的區別介紹

1、__construct構造方法

立即學習PHP免費學習筆記(深入)”;

修改中間控制器類CommonController.class.php 如下:

thinkphp登錄限制時__construct和_initialize的區別介紹

說明:empty() 5.5 版本之后支持表達式,否則會報如下錯誤:

thinkphp登錄限制時__construct和_initialize的區別介紹

運行結果如下:

thinkphp登錄限制時__construct和_initialize的區別介紹

中間控制器必須先構造父類,才能使用父類的方法,修改代碼如下:

thinkphp登錄限制時__construct和_initialize的區別介紹

運行結果:

thinkphp登錄限制時__construct和_initialize的區別介紹

2、_initialize構造方法,如下:

thinkphp登錄限制時__construct和_initialize的區別介紹

通過_initialize構造方法同樣可以實現上述效果,無需構造父類,查看基類控制器代碼如下:

thinkphp登錄限制時__construct和_initialize的區別介紹

通過Controller.class.php中的架構函數可以看出,只要存在 _initialize方法,就會直接調用來初始化控制器,因此ThinkPHP內置

的構造方法無需再構造父類。

推薦教程:《TP5

以上就是

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享