[Php-it] Funzione che deve ritornare molte cose

Jonathan Stoppani st.jonathan at gmail.com
Tue Feb 6 18:28:48 CET 2007


On Feb 6, 2007, at 5:20 , Aldo Galimberti wrote:
>
> usando la forma procedurale ( senza usare oggetti) usavo questo  
> approccio:
>
> function GetUser() {
>   .... codice php che collega dal db
>
>   return retVal;
> }
>
>
> dove retVal poteva assumere:
>
> un valore negativo : errore di db
> zero: utente non trovato
> un valore positivo: user id
> E dal valore di ritorno potevo gestire gli errori.
>
> Con PHP5 in su puoi usare la gestione delle eccezioni per gestire  
> gli errori, molto comoda....
>
> tipo:
>
> try {
>   collego db
>
>  if (retval = 0) throw new exception ("utente non trovato")
>
> }catch (exception e) {
>    echo "errore db: ";
> }
> finally {
>   chiudo db
> }
>
>
> -
> Aldo Galimberti - TXT Polymedia S.p.A.
> v.Frigia 27 - 20126 Milano
> Tel. +39 02 257711 Fax. +39 02 2578994
> http://www.txt.it/

Dubito che fai il finally... :D

Puoi anche estendere exception e fare dei catch diversificati:

UserNotFoundException extends Exception
DatabaseException extends Exception

e poi

try {
	$user = new User($id);
} catch (UserNotFoundException $e) {
	echo "User not foud";
} catch (DatabaseException $e) {
	echo "Database error";
	Logger::error($e->getMessage());
}


Best Regards,
Jonathan Stoppani
---------------------------------
       WWW:  http://www.garetjax.info
       ICQ:  322754291
       AIM:  garetjax a mac.com
MSN+GTalk:  st.jonathan a gmail.com




More information about the Php-it mailing list