CodeIgniter Forums
Calling helpers/classes inside another helper - 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: Calling helpers/classes inside another helper (/showthread.php?tid=6254)



Calling helpers/classes inside another helper - El Forum - 02-20-2008

[eluser]simonspoken[/eluser]
I'm writing a helper at the moment to do some custom session tracking, and I'd like to be able to access parts of the URI using the URI Class.

$this->uri->segment(n) won't work (throws an error about the use of $this) - what will?

EDIT: Turns out you can't use other classes inside helpers. I'll create a class instead. Cheers


Calling helpers/classes inside another helper - El Forum - 02-20-2008

[eluser]m4rw3r[/eluser]
Use the function get_instance(), it will return your controller object.

Ex.
Code:
$CI =& get_instance();
$CI->uri->segment($n);



Calling helpers/classes inside another helper - El Forum - 02-20-2008

[eluser]ejangi[/eluser]
@simonspoken - Just to clarify the reason why $this throws an error inside a helper.

Helper functions are in the global namespace (not inside a class or anything like that) and therefore don't belong to an object. The $this variable is a reserved keyword (built into PHP itself) for accessing the containing object. So, because a helper function isn't inside a class/object PHP has not defined the $this variable and hence the error.

I hope that makes sense.