Re: Raising errors inside a constructor

From: Date: Mon, 16 Aug 2004 06:39:34 +0000
Subject: Re: Raising errors inside a constructor
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-32700@lists.php.net to get a copy of this message
Why a such ugly thing : $foo =& Foo::construct($options); if (PEAR::isError($foo)) { ... } when you could do : $foo = new Foo($options); if ($foo->hasError()) { ... } ?
It's very subjective, of which is better, a) will probably throw a "no such method error" at some point, b) you are more likely to catch a error in a later method call, indicating that the object was not constructed properly..
I don't think it makes anything clearer. My Idea was to make some base class provide this sort of features. See, class Foo extends SomeBaseClass. I must confess I thought about the PEAR base class itself... But, well, as Lukas stated, the most stylish is : try {
    $foo = new Foo($options);
} catch (InvalidOption $e) {
    ...
} It's a very personal decision, but i really dont like the idea of assigments throwing errors too often, it makes the process of reading code, and guessing where you need to catch exceptions a little more complex: (eg. only method calls need to be try'd, or almost anything could throw an exception?)
Regards Alan
-- og


« previous php.pear.dev (#32700) next »