2

I realise this is a big topic, but I'd appreciate some rough ideas on the best practice ways for handling errors, particularly in OO PHP (but also interested in good patterns which transcend language specifics). Let's assume these classes and applications are large scale ones.

I also realise that the most recommended way is to make use of PHPs Exception model, and I'm interested in knowing how the below example might translate into this.

EDIT: what I'm looking for is ideas on how best to handle say the errors generated when validating say form data (not errors with how the method has been called), perhaps the Exception model isn't for this use case END EDIT

Particularly given that not all errors need to be displayed/used immediately (can multiple exceptions be thown in the same method?), some might need to be logged (that is another question I guess), some might only be relevant if other dependencies are true (or false), and some might just be warnings, some might be critical and some might be statuses/notifications.

The current approach I take is something along the lines of:

class ClassName()
{
    public MethodName( $data, &$errors )
    {
        $e = [];

        // validate content and perform data handling
        // any errors should be added to the array $e

        if( empty( $e ) ) {
            return $processed_data;
        } else {
            $errors = $e;
            return false;
        }
    }
}

// and then calling:
$method_call = ClassName::MethodName( $data, $errors );

if( $method_call ) // do something with the data/display success message etc.
else // decide what to do with any errors

Tar Folks,

1 Answer 1

2

It depends really. The "proper" OO way to do that is to use exceptions though. There are already many different types of exception built in (see http://www.php.net/manual/en/spl.exceptions.php) and you can define your own (see http://php.net/manual/en/language.exceptions.php).

A way to use them could be similar to this

function foo($bar)
{
    if(!is_string($bar))
        throw new InvalidArgumentException("String argument expected");

    if(strlen($bar) > 50)
        throw new LengthException("String argument is too long!");
}

Then, in calling code, use try/catch, e.g.

try {
     foo("hjsdkfjhkvbnsjd");
} catch(LengthException $ex) {
     // Trim the string
} catch (InvalidArgumentException $ex) {
     // Try to recover. Cast or trim or something
} catch (Exception $ex) {
     // We hit an exception we're not explicitly handling.  
     // Gracefully exit
     die($ex->Message);
}

In any case, an exception should only be thrown if the method has actually been called wrong, or if the method fails in a fatal way.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok great, so in regards to your last comment I think I should probably have refactored my question. Because what I'm looking for is ideas on how best to handle say the errors generated when validating say form data (not errors with how the method has been called)
In that case, you can do a similar thing. Extend Exception to contain a field for your error elements, then throw it after your validation has completed and failed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.