CodeIgniter Forums
get_instance return null - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: get_instance return null (/showthread.php?tid=63667)



get_instance return null - llobu - 11-25-2015

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';


RE: get_instance return null - InsiteFX - 11-25-2015

Did you place the file in the ./application/core folder?


RE: get_instance return null - llobu - 11-25-2015

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

Sad

In CI2 worked perfectly.


RE: get_instance return null - jvandemerwe - 11-25-2015

(11-25-2015, 07:48 AM)llobu Wrote: 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';

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.


RE: get_instance return null - Narf - 11-25-2015

You registered here just to cross-post from StackOverflow?! Seriously?!


RE: get_instance return null - gastongr - 11-26-2015

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.


RE: get_instance return null - kenjis - 11-26-2015

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

This happened to me as well and it's really frustrating.

If there is no CI instance, you have to create it before using it.

Maybe it is bad design of CI.


RE: get_instance return null - sv3tli0 - 11-27-2015

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

This happened to me as well and it's really frustrating.

If there is no CI instance, you have to create it before using it.

Maybe it is bad design of CI.

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


RE: get_instance return null - gastongr - 11-27-2015

Quote:P.S. And why do you need CI instance inside the exceptions at all ?
Exceptions should not do any logical steps..

Mainly to translate the error message, at least in my case.


RE: get_instance return null - kenjis - 11-27-2015

I think this workround probably works. But I don't test for real.

PHP Code:
$CI =& get_instance();
if (
$CI === null) {
 
   new CI_Controller();
 
   $CI =& get_instance();