【发布时间】:2011-08-22 12:36:20
【问题描述】:
我很高兴有一点变量范围问题。也许我只是需要更多的咖啡......
这是我的(简化的)代码 - 这是在 CodeIgniter 2 中:
class Agent extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('agent_model');
// Get preliminary data that will be often-used in Agent functions
$user = $this->my_auth_library->get_user();
$agent = $this->agent_model->get_agent($user->id);
}
public function index()
{
$this->template->set('info', $this->agent_model->get_info($agent->id));
$this->template->build('agent/welcome');
}
不幸的是,当我运行索引函数时,我被告知:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: agent
Filename: controllers/agent.php
Line Number: 51
第 51 行是索引函数的第一行。怎么了?这是范围问题还是其他问题?
谢谢!
【问题讨论】:
-
你没有设置任何类变量,只是函数变量。见php.net/manual/en/language.oop5.properties.php
标签: php codeigniter variables scope