使用Zend Framework在我的控制器中获取"致命错误:无法访问空属性"

Jho*_*rra 0 php zend-framework

这是一些代码,它是我的控制器的基类,初始化所有常见值.该文件位于我的模型文件夹中,如果这有所不同.奇怪的是它在我正在开发的MAMP本地工作,但不在服务器上.我在想它可能是一个配置问题?

<?php
Zend_Loader::loadClass('Zend_Controller_Action');

class BaseController extends Zend_Controller_Action
{
    protected $auth;
    protected $current_user;
    protected $db;

    protected function initialize_values()
    {
        $auth = Zend_Auth::getInstance();
        if($auth->hasIdentity())
        {
            $this->$current_user = $auth->getIdentity();
            $this->view->user = $this->$current_user;
        }

        $this->db = Zend_Registry::get('dbAdapter');

        $this->view->controller_name = $this->_request->getControllerName();
        $this->view->view_name = $this->_request->getActionName();
    }
}
Run Code Online (Sandbox Code Playgroud)

我把它放在if语句中的第一行

$this->$current_user = $auth->getIdentity();
Run Code Online (Sandbox Code Playgroud)

我理解这个错误意味着它试图访问不存在的属性或方法.在这种情况下,我知道存在

Vik*_*ikk 5

而不是$this->$current_user,你不应该使用$this->current_user

$current_user 在您的情况下为null,因此错误.