dsuvs66406 2014-06-22 23:35
浏览 68
已采纳

致命错误:在LanguageComponent上的非对象上调用成员函数read()

I am having this line in my CakePHP code: $language = $this->Cookie->read('language');

and I'm getting this error:

Fatal error: Call to a member function read() on a non-object in
C:\Apache24\htdocs\cake\app\Controller\Component\LanguageComponent.php on line 27

Here is my LanguageComponent.php code

<?php

//App::Import('Component', 'Cookie'); 
class LanguageComponent extends Object {

    public $controller = null;
    public $components = array('Cookie');
    public $languages = array();

    public function initialize($controller) {
        $this->controller = $controller;
        if (empty($languages)) {
            $this->languages = Configure::read('Config.languages');
        }
        $this->set();
    }

    public function set($language = null) {
        $saveCookie = false;
        if (empty($language) && isset($this->controller)) {
            if (!empty($this->controller->params['named']['lang'])) {
                $language = $this->controller->params['named']['lang'];
            } elseif (!empty($this->controller->params['url']['lang'])) {
                $language = $this->controller->params['url']['lang'];
            }
            if (!empty($language)) {
                $saveCookie = true;
            }
        }
        if (empty($language)) {
            $language = $this->Cookie->read('language');
            if (empty($language)) {
                $saveCookie = true;
            }
        }
        if (empty($language) && !array_key_exists($language, $this->languages)) {
            $language = Configure::read('Config.language');
        }
        Configure::write('Config.language', $language);
        if ($saveCookie) {
            $this->Cookie->write('language', $language, false, '1 year');
        }
    }

}

?>

Where could the problem be?

展开全部

  • 写回答

3条回答 默认 最新

  • douluan8828 2014-06-23 00:51
    关注

    Wrong parent class

    Compare the code in the question:

    class LanguageComponent extends Object {
    

    To any core component:

    class AuthComponent extends Component {
    

    Extending the wrong class means that none of the component constructor logic is invoked, and the way components are loaded on first access will not be available.

    In the process of upgrading?

    This is possibly because the language component was originally written for 1.x - the parent class for components changed when 2.x was released. As mentioned in the 2.0 migration guide:

    Component is now the required base class for all components.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 单片机 TC277 PWM
  • ¥15 在更新角色衣服索引后,Sprite 并未正确显示更新的效果该如何去解决orz(标签-c#)
  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部