CodeIgniter Forums
get_instance now NULL in MY_Exceptions [CI 3.1.5] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forum-17.html)
+--- Thread: get_instance now NULL in MY_Exceptions [CI 3.1.5] (/thread-68828.html)



get_instance now NULL in MY_Exceptions [CI 3.1.5] - skunkbad - 08-31-2017

For years I've been extending the CI Exceptions class, specifically the show_404 method. I load my website template (a view), and then insert the error_404 view into it as a nested view. I'm just wondering what changed since 3.1.3 that would no longer allow me to use get_instance() the same way that I used to, because now I just get NULL, and that's no fun.


Code:
[b]Notice[/b]: Trying to get property of non-object in [b]/var/www/htdocs/houston/application/core/MY_Exceptions.php[/b] on line [b]49[/b]

An uncaught Exception was encountered
Type: Error
Message: Call to a member function set_status_header() on null
Filename: /var/www/htdocs/houston/application/core/MY_Exceptions.php
Line Number: 49
Backtrace:
File: /var/www/htdocs/houston/index.php
Line: 315
Function: require_once

As a work-around, I just used cURL to make a request to my custom error controller. Still would be nice to know what happened, and why get_instance now returns NULL, where it used to have the output and view methods. Is there a solution to this? Ideally, if I use get_instance, I'd get the loaded classes so I can use them.


RE: get_instance now NULL in MY_Exceptions [CI 3.1.5] - skunkbad - 09-14-2017

Crickets?


RE: get_instance now NULL in MY_Exceptions [CI 3.1.5] - Narf - 09-15-2017

There are no changes between 3.1.3 and 3.1.5 related to this.

You should be getting the same behavior in 3.1.3; it just depends on when you call get_instance(). Note that you're getting an instance of the controller, and with you doing this during 404 handling, that doesn't always happen under the same conditions.

When you call show_404() manually, that's most likely on a routed request that you chose to treat as a 404. But the same logic is also executed automatically when CI did NOT find a route, and thus did not instantiate a controller at all; i.e. there's no controller instance to be returned.


RE: get_instance now NULL in MY_Exceptions [CI 3.1.5] - spjonez - 09-15-2017

Probably not the most elegant solution but we do a redirect to our error controller.

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

class 
MY_Exceptions extends CI_Exceptions {
    
/* public methods */

    
public function show_404$page ''$log_error true ) {
        
header'Location: https:/error/404' );

        exit;
    }
}

?>



RE: get_instance now NULL in MY_Exceptions [CI 3.1.5] - skunkbad - 09-15-2017

Well I feel pretty dumb. It was that I forgot to add a value to my 404_override route. Everything works fine now.