У меня есть библиотека CodeIgniter. Когда я получаю к нему доступ из моего контроллера, генерируется PHP Ошибка серьезности: Уведомление. Однако отладчик показывает, что суперобъект существует в библиотеке и является суперобъектом CI.
Вот класс библиотеки:
class Auth_lib {
protected $CI;
public function __construct()
{
$this->$CI =& get_instance();
$this->$CI->load->model('auth_model');
$this->$CI->load->library('session');
}
/**
* checks if the current user is logged into a session
* @param
* @return boolean
*/
public function is_logged_in(){
$result = $this->$CI->session->userdata('is_logged_in');
return $result;
}
}
И вот как библиотека вызывается из моего контроллера:
public function __construct()
{
parent::__construct();
$this->load->helper('url_helper');
$this->load->library('auth_lib');
// test if user is logged in and authorised, if not redirect to login controller
if($this->auth_lib->is_logged_in() != null){
// check if is admin
}
} else {
// not logged in, redirect to login controller
}
}
Итак, почему я получаю эту ошибку:
Обнаружена ошибка PHP
Серьезность: Уведомление
Сообщение: неопределенная переменная: CI
Имя файла: библиотеки / Auth_lib.php
Номер строки: 20
использование $this->CI
вместо $this->$CI
,
return isset($result)?TRUE:FALSE;
из библиотеки is_logged_in()
функция.
библиотеки / Auth_lib.php
class Auth_lib {
protected $CI;
public function __construct()
{
$this->CI =& get_instance();
$this->CI->load->model('auth_model');
$this->CI->load->library('session');
}
/**
* checks if the current user is logged into a session
* @param
* @return boolean
*/
public function is_logged_in(){
$result = $this->CI->session->userdata('is_logged_in');
return isset($result)?TRUE:FALSE;//make sure that you have set session or not
}
}
контроллер
public function __construct()
{
parent::__construct();
$this->load->helper('url_helper');
$this->load->library('auth_lib');
// test if user is logged in and authorised, if not redirect to login controller
if($this->auth_lib->is_logged_in()){
// check if is admin
}
else {
// not logged in, redirect to login controller
}
}
Других решений пока нет …