PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Constructors that can fail
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Constructors that can fail

Réponse
 
LinkBack Outils de la discussion
Vieux 15/02/2008, 15h52   #1
Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Constructors that can fail

This is just a quickie but it's been buggign me for a while now and as
far as I can tell the PHP manual doesn't have anything to say on the
subject.

I have a situation where I have constructors that can fail. For
example ones that fetch a database row and populate the instance
properties with the returned results. If, for example, an invalid ID
gets passed in the new call, then the result can be that no row is
returned and the result is my newly created object has invalid
state.

Instead of this I'd like for the instance to just be set to NULL if
something goes wrong in the constructor. For example, if I do

$item = new Item ($id)

then I want $item to contain a valid instance of Item if the ID was
valid, but for $item to be NULL if it wasn't. Is this possible with
PHP, if so, then how?
  Réponse avec citation
Vieux 15/02/2008, 16h07   #2
Iván Sánchez Ortega
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Constructors that can fail

Gordon wrote:

> then I want $item to contain a valid instance of Item if the ID was
> valid, but for $item to be NULL if it wasn't. Is this possible with
> PHP, if so, then how?


Use a factory design pattern. See
http://en.wikipedia.org/wiki/Factory_method_pattern or have a look at some
programming books.

That way, you'll be able to return an instance, or a NULL value.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Ama un solo día y el mundo entero habrá cambiando.- Robert Browning.
  Réponse avec citation
Vieux 15/02/2008, 16h20   #3
Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Constructors that can fail

On Feb 15, 4:07 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.org> wrote:
> Gordon wrote:
> > then I want $item to contain a valid instance of Item if the ID was
> > valid, but for $item to be NULL if it wasn't. Is this possible with
> > PHP, if so, then how?

>
> Use a factory design pattern. Seehttp://en.wikipedia.org/wiki/Factory_method_patternor have a look at some
> programming books.
>
> That way, you'll be able to return an instance, or a NULL value.
>
> --
> ----------------------------------
> Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
>
> Ama un solo día y el mundo entero habrá cambiando.- Robert Browning.


Thanks. i was hoping it'd be something simple like unset ($this) or
$this = NULL, but the former doesn't seem to do anything and the
latter causes a fatal error.
  Réponse avec citation
Vieux 15/02/2008, 17h07   #4
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Constructors that can fail

On Feb 15, 11:20 am, Gordon <gordon.mc...@ntlworld.com> wrote:
> On Feb 15, 4:07 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
>
>
>
> escomposlinux.-.punto.-.org> wrote:
> > Gordon wrote:
> > > then I want $item to contain a valid instance of Item if the ID was
> > > valid, but for $item to be NULL if it wasn't. Is this possible with
> > > PHP, if so, then how?

>
> > Use a factory design pattern. Seehttp://en.wikipedia.org/wiki/Factory_method_patternorhave a look at some
> > programming books.

>
> > That way, you'll be able to return an instance, or a NULL value.

>
> > --
> > ----------------------------------
> > Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

>
> > Ama un solo día y el mundo entero habrá cambiando.- Robert Browning.

>
> Thanks. i was hoping it'd be something simple like unset ($this) or
> $this = NULL, but the former doesn't seem to do anything and the
> latter causes a fatal error.


Use an exception inside the constructor in conjunction with a factory
method:

class Foo {

protected function __construct($id) {
if($this->loadData($id) === false)
throw new Exception();
}

public static function getFoo($id) {
try {
$obj = new self($id);
} catch (Exception $e) {
$obj = null;
}

return $obj;
}

}
  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 19h22.


É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,12458 seconds with 12 queries