CodeIgniter库:访问CodeIgniter SuperObject的访问导致PHP严重性错误:注意



我有一个代码签名库。当我从控制器访问它时,严重性的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

  1. 使用$this->CI代替$this->$CI

  2. return isset($result)?TRUE:FALSE;来自库的is_logged_in()函数。

    libraries/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
            }
        }
    

最新更新