Welcome Guest, Not a member yet? Register   Sign In
Extending CI_Exceptions
#1

[eluser]xzyfer[/eluser]
Hi All,

before the obligatory 'sigh, not this again', i have read all the relating post and have not found one that dealt with a similar issue.

I am trying to extend CI_Exceptions to add a show_403 function, simple enough dont you think?

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

class My_Exceptions extends CI_Exceptions {

    function __construct()
    {
        parent::CI_Exceptions();
        echo ' loaded My_Exceptions'; //make sure this is being loaded
    }
    /**
     * 403 Access Forbidden Handler
     *
     * @access    private
     * @param    string
     * @return    string
     */
    function show_403($page = '')
    {
        $heading = "403 Access Forbidden";
        $message = "You do not have access to this page.";

        log_message('error', '403 Access Forbidden --> '.$page);
        echo $this->show_error($heading, $message);
        exit;
    }

}
?>

couldn't be simpler... but calling <?php show_403(); ?> in my code results in:

Fatal error: Call to undefined function show_403()

Strangely enough, if i rename my show_403 function to show_404, but keeping the same function body, i get the desired page. This tells me that my extension is being loaded and my extended function is being used, in this case.

However by simply changing the name back to show_403, i get the fatal error again.

What's even stranger is that if i take the above function and add it into the CI_Exceptions file directly, and comment out the show_403 function in my extended class, i get the same Fatal error?!

I thought for a moment that in CI there was some kind of function pooling for all these functions, but that would defeat the dynamic nature of the product, which i am a big fan of btw.

I am at a loss guys, any help would be great!
#2

[eluser]xzyfer[/eluser]
I've a little more investigation and i've noticed that when calling the show_404 it in fact loads the show_404 method from the common.php, so i've simply added a show_403 function to common.php

Code:
/**
* 403 Page Handler
*
* This function is similar to the show_error() function above
* However, instead of the standard error template it displays
* 404 errors.
*
* @access    public
* @return    void
*/
function show_403($page = '')
{
    $error =& load_class('Exceptions');
    $error->show_403($page);
    exit;
}

this works a treat, but is in very bad form.

Am i to understand that the CI_Exceptions library is never explicitly loaded?

To that end, i believe the documentation on displaying errors to be slightly misleading, and IMO developers would benefit from a notice alerting to them to the fact that extended functions to to this library are not accessible via the global constructs, and require you to load/autoload it, as with any other library.

Hope someone can use this and avoid the time i wasted.
#3

[eluser]Randy Casburn[/eluser]
[quote author="xzyfer" date="1224661854"]Fatal error: Call to undefined function show_403()
[/quote]

Hi, You've called an undefined function. If you called the method of an instantiated object of type CI_Exceptions, it would work just fine. You see, you've declared a Method inside a Class declaration. When you call a function in the global space that has not been declared, of-course it cannot be found, so an "undefined function" error is thrown.

Make sure you $this->load->library('Exceptions'); (or your library) first and then...

call $this->show_403($page = '');

Hope this helps,

Randy
#4

[eluser]xzyfer[/eluser]
Thanks for your reply.

I mentioned that in bottom on my reply.

I understand where i went wrong, i just thought it should be noted in the user guide where is error functions such as show_error and show_404 are available in global namespace, that it'd be mentioned that extended methods will require you to explicitly loaded the library. I found this misleading personally.
#5

[eluser]Randy Casburn[/eluser]
Didn't seem to recall that the first time around...

Does seem somewhat misleading I suppose...that is if you overlook the basics of Class/Method declarations. I'm not sure the documentation for CI is there to instruct folks on the very basic nature OO structures. But as I said, it makes sense that there should be consistency between HTML errors.

Randy




Theme © iAndrew 2016 - Forum software by © MyBB