CodeIgniter Forums
Access to CodeIgniter Superobject causes PHP Error of Severity:Notice - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Access to CodeIgniter Superobject causes PHP Error of Severity:Notice (/showthread.php?tid=85940)



Access to CodeIgniter Superobject causes PHP Error of Severity:Notice - BobbyTanomred - 12-22-2022

I have a CodeIgniter library. When I access it from my controller a PHP Error of Severity:Notice is generated. However, the debugger shows that the super object exists in the library and is the CI super object.

Here is the library class:

    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;
    }
}
And this is how the library is called from my controller:

        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
            }

        }
So, why I am getting this error:

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


RE: Access to CodeIgniter Superobject causes PHP Error of Severity:Notice - kenjis - 12-22-2022

In the Auth_lib class, not $this->$CI but $this->CI.


RE: Access to CodeIgniter Superobject causes PHP Error of Severity:Notice - superior - 12-22-2022

Before crossing over to CodeIgniter4 i've used another solution that worked as well.

PHP Code:
<?php 

class Your_class {

    public function __get($instance) {
        return get_instance()->$instance;
    }
  
    
// You do not need:  $this->CI->load->model('name');
    // You can now use:  $this->load->model('name');




RE: Access to CodeIgniter Superobject causes PHP Error of Severity:Notice - kenjis - 12-22-2022

@superior It is nice!


RE: Access to CodeIgniter Superobject causes PHP Error of Severity:Notice - InsiteFX - 12-23-2022

@superior, Great job well done.