set_error_handler in classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

set_error_handler in classes

Post 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. :)
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

with the includes removed, like in your example.. there's nothing that uses register_globals enabled content..
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post 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
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

bump :-/
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Bummer, ok, thanks :)
Post Reply