Welcome Guest, Not a member yet? Register   Sign In
get_instance returns empty object
#1

[eluser]Hartimer[/eluser]
Hi

I'm trying to work with hooks but i'm getting a really anoying error

Code:
Fatal error: Call to a member function helper() on a non-object in /<whatever>/system/application/hooks/my_hook.php on line 6

The hook looks like this:

Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class My_hook {
    public function __construct(){
        $this->obj =& get_instance();
        $this->obj->load->helper('url'); // this is line 6
    }
}

I've seen tones of demos where get_instance works like a charm, including on hooks... have no idea why i'm getting this, but i believe it is a really silly mistake..
#2

[eluser]Hartimer[/eluser]
Shortest problem ever i guess.

The problem was related to the hook type.
I had it set to "pre_controller", changing to "post_controller_constructor" did the trick.
Is it possible however to access CI "pre_controller"?
#3

[eluser]bretticus[/eluser]
How about "post_controller_constructor"?
#4

[eluser]Hartimer[/eluser]
I simply would like to run the hook before any controller gets loaded... performance related concern thats all

EDIT: i meant "controller", not "class"
#5

[eluser]bretticus[/eluser]
have you tried loading the Loader class perhaps?
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class My_hook {
    public function __construct(){
        $this->loader =& load_class('Loader');
        $this->loader->load->helper('url'); // this is line 6
    }
}

Not tested. Not sure that this won't cause errors.

EDIT: on second thought I have gut feeling that this is a bad idea. Smile
#6

[eluser]bretticus[/eluser]
I suppose you cold hack the core (Codeigniter.php) and put in your own hook where you need it:

$EXT->_call_hook('my_pre_controller');
#7

[eluser]Hartimer[/eluser]
I prefer post_controller_constructor. My intent is to extend CI, not change it Smile anyhow, this problem was basically a misconception of the CI inner workings..




Theme © iAndrew 2016 - Forum software by © MyBB