get_instance return null |
Hi friends.
I have started migrating some projects in CI2 to CI3 but it crashed in a simply thing. if I create my extension core for Exceptions: /application/core/MY_Exceptions.php: <?php defined('BASEPATH') OR exit('No direct script access allowed'); class MY_Exceptions extends CI_Exceptions { private $_ci; public function __construct() { parent::__construct(); $this->_ci = &get_instance(); var_dump($this->_ci); } } It returns NULL all the time. I am doing something simple wrong but I don't know what. Any help? echo 'Thanks';
Did you place the file in the ./application/core folder?
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Yes, I have it, and checked the chmod privileges
What I wanted to do is just load 2 lang files and use a template library class MY_Exceptions extends CI_Exceptions { public function show_404($page = '', $log_error = TRUE) { // By default we log this, but allow a dev to skip it if ($log_error) { log_message('error', '404 Page Not Found --> '.$page); } $CI =& get_instance(); $CI->lang->load('error/index'); $CI->lang->load('pages/not_found'); $CI->template->title($CI->lang->line('title')) ->meta('keywords', $CI->lang->line('keywords')) ->meta('description', $CI->lang->line('description')) ->make('errors/not_found'); echo $CI->output->get_output(); exit; } } the var_dump($CI) outputs NULL In CI2 worked perfectly.
(11-25-2015, 07:48 AM)llobu Wrote: Hi friends. Where exactly are you seeing that the instance returns null. I have tried your code and it returns the instance just fine. What I did it making a controller function and called it. It gives me an error that the function is not found, but your class is returning the instance just fine.
Your code will work when you call show_404() or show_error() from any of your controllers, but if the error is dispatched at a higher level like a real 404 this wont work since at that point no CI instance exists.
This happened to me as well and it's really frustrating. (11-26-2015, 11:16 AM)gastongr Wrote: Your code will work when you call show_404() or show_error() from any of your controllers, but if the error is dispatched at a higher level like a real 404 this wont work since at that point no CI instance exists. If there is no CI instance, you have to create it before using it. Maybe it is bad design of CI. (11-26-2015, 02:12 PM)kenjis Wrote:(11-26-2015, 11:16 AM)gastongr Wrote: Your code will work when you call show_404() or show_error() from any of your controllers, but if the error is dispatched at a higher level like a real 404 this wont work since at that point no CI instance exists. If there is exception when CI is loading how do you plan to use CI ? Its logic limitations not bad design.. P.S. And why do you need CI instance inside the exceptions at all ? Exceptions should not do any logical steps.. Best VPS Hosting : Digital Ocean
Quote:P.S. And why do you need CI instance inside the exceptions at all ? Mainly to translate the error message, at least in my case.
I think this workround probably works. But I don't test for real.
PHP Code: $CI =& get_instance(); |
Welcome Guest, Not a member yet? Register Sign In |