변수 유효 범위 문제가 조금 행복합니다. 어쩌면 난 그냥 더 커피를 필요 ...PHP/CodeIgniter - __construct()에서 변수를 설정했지만 다른 함수에서 액세스 할 수 없습니다.
여기내 (간체) 코드는 -이 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은 인덱스 함수의 첫 번째 줄입니다. 무슨 일있어? 이 문제가 범위 문제입니까?
감사합니다. $this->Agent = ...;
및 $this->Agent->id
과 같은 방식으로 액세스 : 당신이 접근은 다음 클래스의 속성, 즉로 설정해야 생성자에 설정된 변수를 원하는 경우
사용하여 얻을 수 있습니다. http://www.php.net/manual/en/language.oop5.properties.php를 참조하십시오. – hakre