Thinkphp 出现 Call to a member function assign() on a non-object
2016-08-12 11:33
537 查看
Call to a member function assign() on a non-object
今天在做东西时遇到了这个问题,调了好久才发现,原来是这样!
出现这个问题的原因是:
我有一个构造函数,但是在这个构造函数中没有调用parent::__construct(); 来实例化父类。
源代码:
开始:<span style="font-size:18px;">public function __construct(){
$this->systemService = new SystemService();
}</span>报错截图:
![](http://img.blog.csdn.net/20160812112424714?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
加上后:<span style="font-size:18px;"><span style="color:#333333;">public function __construct(){
</span><span style="color:#ff0000;">parent::__construct();</span><span style="color:#333333;">
$this->systemService = new SystemService();
}</span></span>这样就好了!~~大家一定要注意啊!~~
简单分析下原因:
我们进入ThinkPHP\Library\Think\Controller.class.php,看到其构造器中有这么一段代码://实例化视图类
$this->view = Think::instance('Think\View');然后我们再看报错的122 行,代码是这样:
就是空的,没有模版实例,所以我们调用 assign ,就相当于 用 null->assign,就会出现我们看到的错误啦!~~
以后可要注意喽!~~~
今天在做东西时遇到了这个问题,调了好久才发现,原来是这样!
出现这个问题的原因是:
我有一个构造函数,但是在这个构造函数中没有调用parent::__construct(); 来实例化父类。
源代码:
开始:<span style="font-size:18px;">public function __construct(){
$this->systemService = new SystemService();
}</span>报错截图:
加上后:<span style="font-size:18px;"><span style="color:#333333;">public function __construct(){
</span><span style="color:#ff0000;">parent::__construct();</span><span style="color:#333333;">
$this->systemService = new SystemService();
}</span></span>这样就好了!~~大家一定要注意啊!~~
简单分析下原因:
我们进入ThinkPHP\Library\Think\Controller.class.php,看到其构造器中有这么一段代码://实例化视图类
$this->view = Think::instance('Think\View');然后我们再看报错的122 行,代码是这样:
$this->view->assign($name,$value);看到这里相信你已经知道原因了吧!~~就是说,如果我们自己有构造函数,而没有调用父类构造函数,对父类进行实例化,那么$this->view
就是空的,没有模版实例,所以我们调用 assign ,就相当于 用 null->assign,就会出现我们看到的错误啦!~~
以后可要注意喽!~~~
相关文章推荐
- 解决ThinkPHP Call to a member function assign() on a non-object
- Call to a member function assign() on a non-object;thinkphp中报错
- 解决ThinkPHP Call to a member function assign() on a non-object
- 使用TP3.2框架时遇到Call to a member function assign() on a non-object
- magento 第四章 出现 "Fatal error:Call to a member function load() on a non-object in \Abstract.php" 问题
- php中出现“Call to a member function Execute() on a non-object”的问题
- php中出现“Call to a member function Execute() on a non-object”的问题
- ThinkPHP Call to a member function display() on a non-object问题的解决
- Fatal error: Call to a member function getSource() on a non-object in G:\wamp\www\ChinaMagicCube\ap
- Fatal error: Call to a member function getSource() on a non-object in G:\wamp\www\ChinaMagicCube\ap
- Fatal error: Call to a member function getSource() on a non-object in G:\wamp\www\ChinaMagicCube\ap
- Fatal error: Call to a member function getSource() on a non-object in G:\wamp\www\ChinaMagicCube\ap
- Fatal error: Call to a member function getSource() on a non-object in G:\wamp\www\ChinaMagicCube\ap
- Fatal error: Call to a member function getSource() on a non-object in G:\wamp\www\ChinaMagicCube\ap
- ECmall错误:Call to a member function get_users_count() on a non-object in /home/dmg/wwwroot/includes/ecapp.base.php
- Fatal error: Call to a member function setAttribute() on a non-object in Magento
- Fatal error: Call to a member function getSource() on a non-object in G:\wamp\www\ChinaMagicCube\ap
- Fatal error: Call to a member function getSource() on a non-object in G:\wamp\www\ChinaMagicCube\ap
- Fatal error: Call to a member function getSource() on a non-object in G:\wamp\www\ChinaMagicCube\ap
- Fatal error: Call to a member function getSource() on a non-object in G:\wamp\www\ChinaMagicCube\ap