Handling Errors in PHP

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-05-07T15:22:45Z Indexed on 2010/05/07 15:38 UTC
Read the original article Hit count: 345

Filed under:
|
|

I have a custom class that, when called, will redirect to a page and send a 'message_type' and 'message' variable via GET. When the page opens it checks for these variables and displays a 'success', 'warning', or 'error' message depending on the 'message_type' variable. I made it so the user thinks they stay on the same page. It also allows for other variables to be passed along with the message.

Is this good practice, or should I just start using exceptions?

Example:

//Call a static function that will redirect to a page, with an error message
RedirectWithMessage::go('somepage.php', MessageType::ERROR, 'Error message here.');

The following checkMessage() function is an include file:

function checkMessage()
{
    if((isset($_GET['message_type']) && strlen($_GET['message_type'])) && (isset($_GET['message']) && strlen($_GET['message_type'])))
    {
        DisplayMessage::display($_GET['message_type'], $_GET['message']);
        return true;
    }
    return false;
}

On the page that is redirected to, call checkMessage();

//If a message is received, display it. If not, do nothing
checkMessage();

I know this might be vague, and I can supply more code if necessary. I guess the issue is that I don't have much experience using exceptions, but I think they seem cumbersome (writing try-catch blocks everywhere). Please let me know if I am making this more difficult for myself or if there is a better solution.

Thanks! Mike

© Stack Overflow or respective owner

Related posts about php

Related posts about exception