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

[eluser]tkrop[/eluser]
I have a custom library:

Code:
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:

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

But at the point where I call
Code:
$CI->load->view()
, it throws an error:

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$user
Filename: libraries/CustomLib.php
Line Number: SEE FIRST CODE EXAMPLE...


$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...
#2

[eluser]xzela[/eluser]
Hi,

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:
Code:
$CI->load->view();

Sorry if I wasn't any help.
#3

[eluser]InsiteFX[/eluser]
WRONG! This is how you do it in helpers.

Code:
if ( ! function_exists('your_name'))
{
    function your_name()
    {
        $CI =& get_instance();
    
        // code what you need.
        $CI->do_something();
    }
}

InsiteFX
#4

[eluser]danmontgomery[/eluser]
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

Code:
$this->load->user

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

[eluser]WanWizard[/eluser]
$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).
#6

[eluser]tkrop[/eluser]
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...
#7

[eluser]tkrop[/eluser]
Ah, the $this reference automatically maps to $this->CI I presume? Smile
#8

[eluser]WanWizard[/eluser]
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