Welcome Guest, Not a member yet? Register   Sign In
Calling helpers/classes inside another helper
#1

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

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

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

[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.




Theme © iAndrew 2016 - Forum software by © MyBB