Re: Raising errors inside a constructor

From: Date: Mon, 16 Aug 2004 17:14:12 +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-32707@lists.php.net to get a copy of this message
Olivier Guilyardi wrote:
why not? It makes the code alot clearer. If you need a single contructor, That may return errrors.. I aways try and use: $x = y::construct(...)
Why a such ugly thing : $foo =& Foo::construct($options); if (PEAR::isError($foo)) { ... } when you could do : $foo = new Foo($options); if ($foo->hasError()) { ... }
What you are describing seems to be PEAR_ErrorStack-style error handling. While there's nothing wrong with this, I think it's good to recognize that if you introduce that for your constructor only (and use return-based error handling elsewhere) then you have created a class that uses different styles of error handling. That could be confusing. If you plan to use stack based error handling everywhere then you aren't really proposing anything new or different ;) -- i.e. AFAIK this is the officla way to implement PEAR_ErrorStack error handling. You could create a baseclass if you wanted; ideally (PHP5) this would be an interface (MyClass implements IStacksErrors). Of course in PHP5 it's a moot point, since there are exceptions. More generally, I'd suggest that perhaps you're trying to do too much in your constructor if you need this feature. With PHP5 it's not a big deal, but in PHP4 it obviously gets ugly. Introducing a mixed-mode error handling just to handle this special case seems like a bad idea (IMHO, of course). Hans

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