Re: Questions about using exceptions
| From: | Sergio Carvalho | Date: | Mon, 04 Jul 2005 17:38:27 +0000 |
| Subject: | Re: Questions about using exceptions | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-38441@lists.php.net to get a copy of this message | ||
Arnold Daniels wrote:
> Hi all,
>
> I've got two questions about using PEAR_Exceptions when building a PHP5
> Pear class:
>
> 1.) When extending a PHP 4 class, should I throw an exception my own
> and work around the existing code?
I imagine you are talking about a scenario where the old package returns
an error value. Something like
class OldClass
{
/**
* @return boolean True if successfull, false otherwise
*/
function foo()
{
(...)
}
}
And testing the return value in the new PHP5 class, like this:
class NewClass extends OldClass
{
public function foo() {
if (!parent::foo()) {
throw new Exception(...);
}
}
}
To fullfill the requirements of the exception usage RFC, the PHP5
package should use only exceptions as error signaling, never error value
returns. This would mean wrapping *every* old method to convert return
values into exceptions.
However, the RFC was written with pure PHP5 packages in mind (i.e.
E_STRICT compliance), not for transitional code. For transitional code,
it may be more trouble than what its worth, morphing all methods to
throw exceptions, not return values.
PHP4 -> PHP5 Transition was never discussed here in the list, but in my
opinion, wrapping the methods is more trouble than what it's worth. The
transitional code shouldn't last more than a couple of years, as
packages evolve into E_STRICT PHP5.
> 2.) Should I only add a @throws note in the DocBlock for exceptions
> which are thrown in that particular function, or also exceptions thrown
> in other functions called by this function?
Documenting all exceptions is a recommendation, but not a requirement in
the exceptions RFC. You should do it, as it provides a complete
description of error behaviour for the method. It is not required,
because it can be a lot of work, and rules should be easy to follow.
Ideally, phpdoc should infer it from the code, but that would be too
good to be true ;-)
Cheers,
Sérgio Carvalho