Welcome Guest, Not a member yet? Register   Sign In
get_instance return null
#1

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';
Reply
#2

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 )
Reply
#3

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.
Reply
#4

(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.
Reply
#5

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

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

(This post was last modified: 11-26-2015, 02:13 PM by kenjis.)

(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.
Reply
#8

(This post was last modified: 11-27-2015, 02:08 AM by sv3tli0.)

(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..
Best VPS Hosting : Digital Ocean
Reply
#9

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.
Reply
#10

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();

Reply




Theme © iAndrew 2016 - Forum software by © MyBB