Page 1 of 1

set_error_handler in classes

Posted: Sat Jan 15, 2005 12:59 am
by Steveo31
I'm new to custom error reporting, but here's a function I adapted from PHP and MySQL Web Development:

Code: Select all

function custom_error($errnum, $errstring, $file, $lineno){
    switch($errnum){
    case 1:
        $this->errorї] = "A fatal error has occured.  All scripts have been terminated and an error report is being filed.";
        //include 'error_report.php';
    break;
    case 2:
        $this->errorї] = "A non-fatal error has occured.  While not a threat to what you were doing, it has caused the scripts to
         terminate.  An error report is being filed.";
        //include 'error_report.php';
    break;
    case 4:
        $this->errorї] = "A parse error has occured.  This means there is an error in the internal structure of the code.  We apologize
        for the inconvenience.";
        //include 'error_report.php';
    break;
    case 8:
        $this->errorї] = "We're sorry, but an error has occurred.  An error report is being made.";
        //include 'error_report.php';
    break;
    case 256:
        $this->errorї] = "A user triggered error has occured.  To better serve you, it's imperative that you let us know what you were doing when
              the error occurred.";
        //include 'error_report.php';
    break;
    case 512:
        $this->errorї] = "A user triggered error has occured.  To better serve you, it's imperative that you let us know what you were doing when
              the error occurred.";
        //include 'error_report.php';
    break;
    case 1024:
        $this->errorї] = "A user triggered error has occured.  To better serve you, it's imperative that you let us know what you were doing when
              the error occurred.";
        //include 'error_report.php';
    break;
}
However, the book isn't all that great cause it's built off of register_globals being on, so I doubt the function works in the first place.

Anyway, once it does work, I hope you can see what I'm trying to do. Based on the errorno, i.e. 1 being E_ERROR, 512 being E_USER_WARNING, etc. it will throw the error into an array, then I'll check it later, based on there continue or break, etc.

Sooooooo, I guess I'm asking how I can make this function work without register_globals. The function parameters seem... off to me to say the least. Doesn't say anything in the book about an alternative. Once the basic function works, I'd like to throw it into a class so when runtime errors occur with class functions, theses errors will show and not the ugly ones.

In a lil over my head, hopefully yall can help. :)

Posted: Sat Jan 15, 2005 8:48 am
by magicrobotmonkey
does the function work as is? If not, what happens. Generally good to run a bit of code before asking for help.

That said, the only variable this function deals with that is not passed to it is $this->error[]. So I would assume it is part of an object. The thing is, this function doesn't care wether register globals is on or off as all its variables are either passed to it or set in its object. The place to worry about reister globals is where those external variables are being set.

Posted: Sat Jan 15, 2005 9:05 am
by feyd
with the includes removed, like in your example.. there's nothing that uses register_globals enabled content..

Posted: Sun Jan 16, 2005 2:15 am
by Steveo31
Yea, I made a little test... works ok I spose. Limited version:

Code: Select all

<?php

ini_set("display_errors", "Off");
function custom_error($errnum, $errstring, $file, $lineno)&#123;
        echo "Error number ".$errnum." ON LINE ".$lineno." OF ".$file."<br />$errstring<hr />";
    &#125;
set_error_handler("custom_error");

include 'nofile';
?>
Very ugly, I know. Doesn't matter in this case though ;)

Anyway, If there is an error, such as move_uploaded_file fails, I'd like to show a custom error, but still have the page continue. The way I've done this in the past was to have a function return false, then check that value. Based on that do something. i.e.

Code: Select all

function bad()&#123;
   return false;
&#125;
if(bad() === false)&#123;
   echo "It's false!!";
&#125;else&#123;
   echo "Yaaay!!";
&#125;
Buuuut, considering this is the actual error being altered, and not one of my own functions, I'm not sure about how to go about it. Kinda vague question, but I hope you can point me in the right direction. :)

-Steve

Posted: Mon Jan 17, 2005 1:55 am
by Steveo31
bump :-/

Posted: Mon Jan 17, 2005 2:17 am
by feyd
All errors matching your error_reporting level will display through your custom error handler. Warnings and notices will continue to run code. Fatal errors, and other stopping errors will still stop the page, and they should.

Posted: Mon Jan 17, 2005 2:38 pm
by Steveo31
Bummer, ok, thanks :)