2017-02-20 51 views
0

我有一個CodeIgniter庫。當我從我的控制器訪問它時,會生成嚴重性:Notice的PHP錯誤。但是,調試器顯示超級對象存在於庫中,並且是超級對象。CodeIgniter庫:訪問CodeIgniter Superobject導致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 $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 
      } 

     } 

那麼,爲什麼我收到此錯誤:

A PHP Error was encountered Severity: Notice Message: Undefined variable: CI Filename: libraries/Auth_lib.php Line Number: 20

+0

嘗試'$這個 - 的> CI'代替'$此 - > $ CI' – MonkeyZeus

+0

如果如果你使用'$ this - > $ CI =&get_instance();'然後在使用'$ CI'之前,你必須聲明'$ CI ='CI';' – MonkeyZeus

+0

你有一個額外的括號後//檢查是否是admin – Callombert

回答

2
  1. 使用$this->CI而不是$this->$CI

  2. 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 
         } 
    
        } 
    
相關問題