Welcome Guest, Not a member yet? Register   Sign In
Loading CI instance inside helper, causes problem in library?

I have a custom library:

class CustomLib {
    var $CI;
    function CustomLib() {
        $this->CI =& get_instance();

    private $user;
    function User() {
        if(!$this->user) { //ERROR IS AT THIS LINE, SEE BELOW...

And I have a custom helper:

if ( ! function_exists('customhelper'))
    function customhelper() {
        $CI =& get_instance();
        switch($CI->customlib->User()->type) {
            case USERTYPE_ADMIN : {
                $bar = $CI->load->view('shared/menu_admin');
            default : {
                $bar = $CI->load->view('shared/menu_guest');
        return $bar;

But at the point where I call
, it throws an error:

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$user
Filename: libraries/CustomLib.php

$this->user should only be checking CustomLib->user, and not the $CI->user right?? Am I doing something wrong here?

If I do print_r($this); then I see that it is a CustomLib object, however it still tries to access CustomLib->CI->user while I actually call CustomLib->user...


I'm no expert but, I don't think helpers can use 'get_instance()'.
What is it getting an instance of?

It may just be a fluke that you're code works all the way to:

Sorry if I wasn't any help.

WRONG! This is how you do it in helpers.

if ( ! function_exists('your_name'))
    function your_name()
        $CI =& get_instance();
        // code what you need.


I think there's some confusion about exactly what the error is... the only CI_Loader object is load (eg, $this->load->library('a_library')Wink... The CI object is whatever the controller is... Something else is going on here. It sounds like you have something like


But that wouldn't really make sense. What does loading the view have to do with the custom library? Can you post that code?

$this actually points to the CI loader, this is how you can access all libraries, models, etc.
You call the User() method of the customlib before loading the view, so the view is not the issue.

I think the issue here is that you declare the variable $user, but you don't assign it a value so it doesn't actually exist (use isset(), you will see it returns false).

I understand the confusion...

This is what happens:

1. CustomLib and customhelper are autoloaded.
2. I call <?=customhelper()?> in code
3. customhelper() creates $CI variable (function scope) by reference
4. customhelper() calls $CI->customlibrary->User() function
5. CustomLibrary constructor creates $CI variable (class scope) by reference

The CustomLibrary->User() function then calls '$this(->user)', which should point to the CustomLibrary instance. However '$this' contains the $CI reference and $this->user only exists in CustomLibrary...

Ah, the $this reference automatically maps to $this->CI I presume? Smile

There isn't really something like '$this->CI'.

The call to the 'get_instance()' function returns a pointer to the CI 'root' object (how depends a bit on if you use PHP4 or PHP5).

When a controller is loaded, all loaded libraries and models are copied (by reference) to the controller, so you can still use things like '$this->load->', without ever defining this variable in your controller.

CI isn't really consistent in the way it handles this reference business, so it gets confusing when you should use $CI and when not.

Theme © iAndrew 2016 - Forum software by © MyBB