Welcome Guest, Not a member yet? Register   Sign In
get_instance now NULL in MY_Exceptions [CI 3.1.5]

(This post was last modified: 09-01-2017, 12:10 PM by skunkbad.)

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.

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


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.

(This post was last modified: 09-15-2017, 05:30 AM by spjonez.)

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

MY_Exceptions extends CI_Exceptions {
/* public methods */

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



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

Theme © iAndrew 2016 - Forum software by © MyBB