|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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; } } |
|
![]() |
| Outils de la discussion | |
|
|