Welcome Guest, Not a member yet? Register   Sign In
Can't extend the core exceptions class
#1

[eluser]Rushino[/eluser]
Hello,

I am trying to use a method in an helper file that is located in my MY_Exceptions class that extend the exceptions class from CI.

Code:
class MY_Exceptions extends CI_Exceptions
{
    function __contruct()
    {
        parent::__contruct();
    }


     function a_method()
     {
       // i can't get access to this !
     }
}

But that doesnt work, i can't access this method.. it say that its not declared.

Any ideas ?

Thanks.
#2

[eluser]toopay[/eluser]
Where you save MY_Exceptions.php?
#3

[eluser]Rushino[/eluser]
In the application/core directory.

I do believe that the CI_Exceptions class isnt loaded automaticly. The function show_errors(), show_404() are all functions called from the Common.php file of the core of CI and not the actual CI_Exceptiions class.

Its look like that the only way is to load that file.. but that go against the principle of extending those core class.. this should be corrected in the help file.
#4

[eluser]toopay[/eluser]
You cannot access a private function! But you can override the Exceptions class function, like this.
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Exceptions extends CI_Exceptions {
    public function __construct()
    {
        parent::__construct();
    }
    
    function show_404($page = '', $log_error = TRUE)
    {
        
        $heading = "404 Page Not Found";
        $message = "From MY_Exception class : The page you requested was not found.";

        // By default we log this, but allow a dev to skip it
        if ($log_error)
        {
            log_message('error', '404 Page Not Found --> '.$page);
        }

        echo $this->show_error($heading, $message, 'error_404', 404);
        exit;
    }
}
#5

[eluser]Rushino[/eluser]
I tried your code but this cause an infinite loop. Sad but it now enter the function if i redefine an existing one.

But i guess i should use a library instead if i want custom errors methods that use the ones from code igniter.. i though we could add new methods by extending the exceptions class but it look like we can only redefine them.

We are also stick to the parameters.
#6

[eluser]toopay[/eluser]
[quote author="Rushino" date="1305234406"]I tried your code but this cause an infinite loop.[/quote]

Above code just add 'From MY_Exception class :', in order to show how we can override the exception class function. What you mean with infinite loop?

[quote author="Rushino" date="1305234406"]i though we could add new methods by extending the exceptions class but it look like we can only redefine them. [/quote]

You can add new method, why not. But the exception class is intend to use as error management at core class, and it intend to work at "system" scope, not at controller scope.

[quote author="Rushino" date="1305234406"]We are also stick to the parameters.[/quote]

You can define your own parameter, but it force you to redefine all related core class, and i will need strong reasons to do that such extra job.
#7

[eluser]Rushino[/eluser]
With this code..

Code:
class MY_Exceptions extends CI_Exceptions {


    function __construct()
    {
        parent::__construct();
    }

     public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
    {
         $this->show_error($heading, $message, 'error_general', 500);
    }
}

I get this error.

Fatal error: Maximum function nesting level of '100' reached, aborting! in G:\wamp_www\cigestion\application\core\MY_Exceptions.php on line 50

If i remove the MY_Exceptions its all work fine.

And if i change the line

Code:
$this->show_error($heading, $message, 'error_general', 500);

for

Code:
parent::show_error($heading, $message, 'error_general', 500);

Nothing appear..

Strange. Is there anything i am doing wrong ?
#8

[eluser]toopay[/eluser]
First, i dont give you that such example (check above!) Second, you did generates infinite loop, since you didnt put...
Code:
exit;
at your code.
#9

[eluser]Rushino[/eluser]
I added the exit() and this didnt affect anything..

Code:
public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
    {

         $this->show_error($heading, $message, 'error_general', 500);
          exit;
    }
#10

[eluser]Rushino[/eluser]
I give up. I tried everything possible for like 4 hours, seen many exemples, seen many poeple that seemed to have the same problem than me with the exception class. So at the end, i am better off with a library anyway since my goal was to make a new method which handle any app errors. This library will simply call log_message or show_error depending on the parameters passed to that new method.

But thanks a lots for your help !




Theme © iAndrew 2016 - Forum software by © MyBB