Welcome Guest, Not a member yet? Register   Sign In
Access to CodeIgniter Superobject causes PHP Error of Severity:Notice
#1

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
Reply
#2

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

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');

Reply
#4

@superior It is nice!
Reply
#5

@superior, Great job well done.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB