[PHPwestoz] Hellooooo? (error handling)

Nicolas Connault nicou at sweetpeadesigns.com.au
Tue Jan 25 14:36:02 UTC 2005


Thanks for your input Sam. Take a look at this error-handling class:
http://nicoconnault.users.phpclasses.org/browse/package/345.html?download=zip

It's the best PHP4 solution I've found so far, and sooo easy to set up. 
YOu just have to create the object at the beginning of the script, and 
it runs all by itself, no need for method calls.

Nicolas

> Well, I don't know about OOP, but I generally use:
>
> <?php
>
> $errors = array();
>
> if (error1($_POST['field']))
> {
>    $errors['field'][] = "This is a horrible error";
> }
>
> if (error2($_POST['field']))
> {
>    $errors['field'][] = "This is another horrible error";
> }
> else if (error3($_POST['field']))
> {
>    $errors['field'][] = "This is yet another horrible error";
> }
>
> if (empty($errors['field']))
> {
>    // handle data
> }
>
> // ...
>
> ?>
> <form>
> <?=(!empty($errors) ? '<p>There were errors. Please fix and try 
> again.</p>' : '')?>
> Field: <input type="..." name="field" value="<?=$_POST['field']?>" 
> /><br />
> <?php if (!empty($errors['field'])) { ?>
> <ul><li><?=implode('</li><li>', $errors['field'])?></li></ul>
> <?php } ?>
> <!-- ...and repeat -->
> </form>
>
>
> This allows for great flexibility in terms of error checking, and also 
> in presentation, as the error array is just a bunch of strings.
>
> I also tend to use $errors[''] or $errors['.'] as a gobal errors 
> field, presented at the top of the form, or such.
>
> This could be converted into an object-orientated solution, depending 
> on how you're handling your data inputs - for example, an OOP forms 
> implentation coule implement error members on all objects, and check 
> for the existence of errors when outputting the form. Errors could be 
> added to form objects, as well as input objects. validate() and 
> handle() (or equiv.) could add to these. The error member could either 
> be a static string array, or an array of Error objects, your choice.
>
> There you go :P
>
> -- Sam






More information about the PHPwestoz mailing list