Loading CI instance inside helper, causes problem in library? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Loading CI instance inside helper, causes problem in library? (/showthread.php?tid=29310) |
Loading CI instance inside helper, causes problem in library? - El Forum - 04-05-2010 [eluser]tkrop[/eluser] I have a custom library: Code: class CustomLib { And I have a custom helper: Code: if ( ! function_exists('customhelper')) But at the point where I call Code: $CI->load->view() Quote:A PHP Error was encountered $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... Loading CI instance inside helper, causes problem in library? - El Forum - 04-05-2010 [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. Loading CI instance inside helper, causes problem in library? - El Forum - 04-06-2010 [eluser]InsiteFX[/eluser] WRONG! This is how you do it in helpers. Code: if ( ! function_exists('your_name')) InsiteFX Loading CI instance inside helper, causes problem in library? - El Forum - 04-06-2010 [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')... 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? Loading CI instance inside helper, causes problem in library? - El Forum - 04-06-2010 [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). Loading CI instance inside helper, causes problem in library? - El Forum - 04-06-2010 [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... Loading CI instance inside helper, causes problem in library? - El Forum - 04-13-2010 [eluser]tkrop[/eluser] Ah, the $this reference automatically maps to $this->CI I presume? Loading CI instance inside helper, causes problem in library? - El Forum - 04-14-2010 [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. |