您的位置:首页 > 编程语言 > PHP开发

thinkphp中__construct构造函数应用举例心得分享

2014-08-20 15:14 218 查看
thinkphp中的__construct是不可以随便用的,因为你的模块类继承上级类,上级类有定义好的__construct看代码:

这样定义是错的
public function __construct(){
$this->checkPermission();
}


这样是对的:
public function __construct(){
parent::__construct();
$this->checkPermission();
}


PHP手册有说明:

-------------------------------------------------------------------------------------------------------

Note: 如果子类中定义了构造函数则不会暗中调用其父类的构造函数。要执行父类的构造函数,需要在子类的构造函数中调用 parent::__construct()。

-------------------------------------------------------------------------------------------------------

看Action类
/**
+----------------------------------------------------------
* 架构函数 取得模板对象实例
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function __construct()
{
//实例化视图类
$this->view       = Think::instance('View');
//控制器初始化
if(method_exists($this,'_initialize'))
$this->_initialize();
}


如果发现代码出错请确认是否有上述错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: