Re: __toString()
| From: | Jon Parise | Date: | Mon, 15 Dec 2003 06:35:10 +0000 |
| Subject: | Re: __toString() | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-6458@lists.php.net to get a copy of this message | ||
On Mon, Dec 15, 2003 at 08:09:12AM +0200, Andi Gutmans wrote:
> >Hopefully, this still works (don't have a recent PHP5 build to try):
> >
> > print (string)$defect->thrownException();
> >
> >The explicit cast also addresses this case:
> >
> > print (string)$defect->thrownException() . "\n";
>
> Nope. How is it different or nicer than doing
> $defect->thrownException()->__toString()?
> It saves you 4 characters?
It's a contrived example. Perhaps it would make more sense if I
phrased it like this:
function printException($e)
{
print (string)$e;
}
... where $e could be a string or an object. The alternative is to be
explicit:
function printException($e)
{
if (is_object($e) && method_exists($e, '__toString')) {
print $e->__toString();
} else {
print $e;
}
}
I don't belong to the "character saving" camp. I'm really after
expressive code that hides away administrative machinery whenever
possible.
--
Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)