Jhorra
Jhorra

Reputation: 6321

Getting "Fatal error: Cannot access empty property" in my controller using the Zend Framework

This is the bit of code, it's a base class for my controller that initializes all common values. The file is in my models folder if that makes a difference. The weird thing is it works locally in MAMP where I'm developing, but not on the server. I'm thinking it may be a configuration issue?

<?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();
    }
}

I get it on the line that is first in the if statement

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

I understood this error to mean it was trying to access a property or method that doesn't exist. In this case I know that exists

Upvotes: 0

Views: 1381

Answers (2)

lloiacono
lloiacono

Reputation: 5050

@Jhorra If you use $this->$current_user you are referencing to a property of the object $this, an the property you are trying to access is whatever $current_user was set to.

Let's say you have something like this:

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

But in your case you are assigning some value to $current_user and is possible that that value is not a property of the $this object.

Upvotes: 0

Vikk
Vikk

Reputation: 3363

Instead of $this->$current_user, shouldn't you use $this->current_user

$current_user is null in your case, hence the error.

Upvotes: 5

Related Questions