Welcome Guest, Not a member yet? Register   Sign In
Accessing $this in a helper?
#1

[eluser]antiver[/eluser]
I created a user helper that contains functions such as login(), logout(), authenticated(), get_user(), etc. When I went to use it (no, I didn't test it before it was done lol), it threw a fatal error that I was using $this in a non-object context. Fair enough. I was trying to use CodeIgniter's session library, and to access that, I was using $this->session.

My first thought was to try and get $this into my functions, perhaps by
Code:
//usage: login($this);
function login($that) {
  $that->session->...
}
But I assume that'll just make a local copy of $this.

Now I'm thinking that I could fix my problem by rebuilding my user helper as a library, but that doesn't seem like an entirely logical solution and I was wondering if there was something I missed - where should I have written my user functions? Thanks.
#2

[eluser]gtech[/eluser]
I think you can get a copy of this in a library or helper by getting the CI instance
Code:
$CI =& get_instance();
$CI->load->view('header');

if you look at the form helper it uses & get_instance();
#3

[eluser]antiver[/eluser]
Ooh, thanks I'm trying that right now.




Theme © iAndrew 2016 - Forum software by © MyBB