Re: Raising errors inside a constructor

From: Date: Mon, 16 Aug 2004 18:47:47 +0000
Subject: Re: Raising errors inside a constructor
References: 1 2 3 4 5  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-32710@lists.php.net to get a copy of this message
Olivier Guilyardi wrote:
Hi Hans, Hans L wrote:
$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.
As I stated in my last reply to Alan, I would use the same style of error handling for both methods and the constructor. Additionally, this would make it nicer for methods, using the usual false return value (as in 99% of the php api functions) : if (!$bar = $foo->bar()) {
    echo $bar->popError();
}
Ok, so what you have proposed is simply "why not use PEAR_ErrorStack for error handling". Of course you can do this! -- this is an official PEAR error handling option. It sounds like you haven't seen these docs: http://pear.php.net/manual/en/core.pear.pear-errorstack.intro.php Also, as you can see in the example you don't even have to create a base class since you can use PEAR_ErrorStack statically. You're welcome to design your packages however you want (within coding style limits, of course), but there's no advantage to anyone else's code in creating such a baseclass if you are the only one using it. Hans

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