获取“致命错误:无法访问空属性”;在我的控制器中使用Zend框架


Getting "Fatal error: Cannot access empty property" in my controller using the 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();
    }
}

我在if语句的第一行得到它

$this->$current_user = $auth->getIdentity();

我理解这个错误意味着它试图访问一个不存在的属性或方法。在本例中,我知道

不应该用$this->$current_user,而应该用$this->current_user

$current_user在您的情况下是空的,因此错误。

@Jhorra如果你使用$this->$current_user,你引用的是对象$this的属性,你试图访问的属性是任何$current_user被设置为。

假设你有这样的东西:

$current_user = 'name';
$this->$current_user; //this is the same as $this->name

但是在您的情况下,您正在分配一些值给$current_user,并且该值可能不是$this对象的属性