Call to a member function fetch(

2017-06-27  本文已影响0人  LeeBoot
主题:thinkPHP中__construct()带来的$this->fetch()Call to a member function fetch() on null错误

学习到视图的时候,使用的是命名空间是方式,不是依赖注入的方式
use think\Controller;
public function viewController(){
return $this->fetch();
}

这段代码是没有毛病的,当然它返回的都是<strong>字符码</strong>

但是在viewController方法之前,由于是学习使用,前面有好多方法包括__construct(),由于构造函数中没有使用parent::__construct()导致上面的viewController()调用后返回的是:

use think\Controller;
public function __construct(){
config('before','beforeAction');
}
public function viewController(){
return $this->fetch();
}
Call to a member function fetch() on null
调用一个成员函数fetch()在null

然后回到父级的controller中查看__construct(),果然里面用了单例模式实现了fetch()方法创建view实例,所以解决这个问题就是派生控制器继承父级的__construct()方法就可以了,代码改造如下

use think\Controller;
public function __construct(){
parent::__construct();
config('before','beforeAction');
}
public function viewController(){
return $this->fetch();
}

综上,代码出问题都是写代码的过程中的粗心大意,以及规范意识不强所造成的!需要增强代码规范意思,蜕变出自我思想!

上一篇 下一篇

猜你喜欢

热点阅读