Thinkphp实例化有继承类的时候找不到方法报错的解决
当我们在使用一个继承类的__construct()方法后,就发现,父类的方法都不能用了?这个是因为什么呢?
熟悉php类的同学,就会发现,没有运行父类的__construct()方法,原因这是如此
下面的代码参考,thinkphp封装了_initialize方法,正是为了解决父类的继承问题
<?php namespace Home\Controller; use Think\Controller; class CommonController extends Controller { private $login;//用户登陆标识 public function _initialize(){ //parent::__construct();//先初始化父类 ///aaa(); //bbb(); //$this->daohang='index'; // $this->display('a'); //$this->show('这个是全局common', 'utf-8', 'text/xml'); //定义全局缓存时间等等 $this->index(); //这个地方后期记录验证成功的日志 }
同样的问题,如果不在thinkphp中,那样,我们怎么来解决呢?
下面的代码说明一切,只需要
parent::__construct();手动实例化即可,这样父类的方法,大家又可以用了
public function __construct(){ parent::__construct();//先初始化父类 ///aaa(); //bbb(); //$this->daohang='index'; // $this->display('a'); //$this->show('这个是全局common', 'utf-8', 'text/xml'); }
扫描二维码推送至手机访问。
版权声明:本文由学无止境-开拓创新-ipvb学习网发布,如需转载请注明出处。