I've just started looking at hooks today not 100% sure what I'm doing wrong but I'm getting an error when I try and use the $ci object in my function.
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: hooks/language.php
Line Number: 12
My hooks file looks like this. It's in the hooks directory in my application folder.
class Language{
var $ci;
public function __construct(){
$this->ci =& get_instance();
}
function get_language(){
echo $this->ci->session->userdata('language');
}
}
I need to get the value in the session to use in my function. Am I not supposed to do it like this?
Thanks you!
In the Base4/5.php file the get_instance() function is written and it is conditionally loaded so it won’t be present until after it is loaded. And that's the reason its giving error.
Just done another Google search and it seems the Hook I was using Pre Controller was before the object was created I've changed the hook and it seems to work fine now.
I used post_controller_constructor
for my hook, then the CI worked.
And I had to turn on hooks in the config.
Below is default application/config/hooks.php
// Stores the requested URL, which will sometimes be different than previous url
$hook['post_controller'][] = array(
'class' => 'App_hooks',
'function' => 'save_requested',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
// Allows us to perform good redirects to previous pages.
$hook['post_controller'][] = array(
'class' => 'App_hooks',
'function' => 'prep_redirect',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
// Maintenance Mode
$hook['post_controller_constructor'][] = array(
'class' => 'App_hooks',
'function' => 'check_site_status',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
/* End of file hooks.php */
/* Location: ./application/config/hooks.php */
I have changed it to below and it works fine for me
// Stores the requested URL, which will sometimes be different than previous url
$hook['post_controller_constructor'][] = array(
'class' => 'App_hooks',
'function' => 'save_requested',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
// Allows us to perform good redirects to previous pages.
$hook['post_controller'][] = array(
'class' => 'App_hooks',
'function' => 'prep_redirect',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
// Maintenance Mode
$hook['post_controller'][] = array(
'class' => 'App_hooks',
'function' => 'check_site_status',
'filename' => 'App_hooks.php',
'filepath' => 'hooks',
'params' => ''
);
/* End of file hooks.php */
/* Location: ./application/config/hooks.php */