PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > best practices in error handling in PHP
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
best practices in error handling in PHP

Réponse
 
LinkBack Outils de la discussion
Vieux 12/03/2008, 15h18   #1
It Maq
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut best practices in error handling in PHP

Hi,

I want to know what is the best solution for handling errors. After reading some documents dealing with the subject, i have three options:
* Using a class for error handling
* Using PEAR error object
* Using try and catch exceptions

The error handling i want to implement will be done in a big application that was developed from the beginning without any concern about error handling.

Thank you




__________________________________________________ __________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping
  Réponse avec citation
Vieux 12/03/2008, 15h30   #2
Richard Heyes
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] best practices in error handling in PHP

It Maq wrote:

> I want to know what is the best solution for handling errors. After
> reading some documents dealing with the subject, i have three
> options:
> * Using a class for error handling


No point IMO when you could use either the below two.

> * Using PEAR error object


I wouldn't advise it.

> * Using try and catch exceptions


In a new app, or (particularly) if you're adapting existing code, this
is the one I'd recommend.

> The error handling i want to implement will be done in a big
> application that was developed from the beginning without any concern
> about error handling.


Aren't they all? :-/

--
Richard Heyes
Employ me:
http://www.phpguru.org/cv
  Réponse avec citation
Vieux 12/03/2008, 15h31   #3
Christoph Boget
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] best practices in error handling in PHP

> I want to know what is the best solution for handling errors. After reading some
> documents dealing with the subject, i have three options:
> * Using a class for error handling
> * Using PEAR error object
> * Using try and catch exceptions
> The error handling i want to implement will be done in a big application that was
> developed from the beginning without any concern about error handling.


It seems to me that 1 and 3 aren't necessarily mutually exclusive. I
believe that you can use your own error classes in php's try/catch
functionality. As for PEAR's error object, unless you are using PEAR
elsewhere in your app I'm not sure it would be worthwhile to just use
this very small piece.

This is all just IMO.

thnx,
Chris
  Réponse avec citation
Vieux 12/03/2008, 15h49   #4
Zoltán Németh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] best practices in error handling in PHP

2008. 03. 12, szerda keltezéssel 07.18-kor It Maq ezt Ãrta:
> Hi,
>
> I want to know what is the best solution for handling errors. After reading some documents dealing with the subject, i have three options:
> * Using a class for error handling
> * Using PEAR error object
> * Using try and catch exceptions


I would go for try/catch, define different exception classes if
different actions need to be taken, catch them based on their class to
take appropriate action, and catch anything else in the top layer.

greets,
Zoltán Németh

>
> The error handling i want to implement will be done in a big application that was developed from the beginning without any concern about error handling.
>
> Thank you
>
>
>
>
> __________________________________________________ __________________________________
> Looking for last minute shopping deals?
> Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping


  Réponse avec citation
Vieux 12/03/2008, 19h55   #5
petersprc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: best practices in error handling in PHP

try / catch is the most versatile and has the advantage of giving you
a stack trace as well as being able to differentiate between different
error types easily. This is a really cool feature of PHP5.

Now, if you are using PEAR modules or some code that relies on
trigger_error, you may want to customize how those errors are handled.
For example:

[MyScript.php]
<?php

require_once('ErrorHandler.php');
require_once('PearErrorHandler.php');

ErrorHandler::init();
PearErrorHandler::init();

?>

[ErrorHandler.php]
<?php

class ErrorHandler
{
static function init()
{
set_error_handler('ErrorHandlerProc');
}
}

function ErrorHandlerProc($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
return;
}
$msg = "Error: $errstr in $errfile on line $errline.";
throw new Exception($msg);
}

?>

[PearErrorHandler.php]
<?php

require_once('PEAR.php');

class PearErrorHandler
{
static function init()
{
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK,
'PearErrorHandlerProc');
}
}

function PearErrorHandlerProc($err)
{
$str = $err->getMessage() . ' (' . $err->getUserInfo() . ')';
throw new Exception($str);
}

?>

On Mar 12, 10:18 am, itmaqu...@yahoo.com (It Maq) wrote:
> Hi,
>
> I want to know what is the best solution for handling errors. After reading some documents dealing with the subject, i have three options:
> * Using a class for error handling
> * Using PEAR error object
> * Using try and catch exceptions
>
> The error handling i want to implement will be done in a big application that was developed from the beginning without any concern about error handling.
>
> Thank you
>
> __________________________________________________ __________________________________
> Looking for last minute shopping deals?
> Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h48.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,28501 seconds with 13 queries