[PHPTAL] Usage of __toString magic method
Kornel Lesiński
kornel at geekhood.net
Sun Jun 5 18:16:31 CEST 2011
On Sun, 05 Jun 2011 15:58:06 +0100, Manu Robledo <molists at mopot.de> wrote:
> I came a cross some problems, which were caused by PHPTAS relying on the
> __toString() magic method, when converting objects to strings.
>
> First problem: Exceptions can´t be thrown within __toString(), see:
> <http://stackoverflow.com/questions/2429642/why-its-impossible-to-throw-exception-from-tostring>
That's the contract of __toString, so you shouldn't even try to throw
exceptions from this method, even if you can get away with it sometimes.
Is there a strong use-case for throwing exceptions at point of conversion
to string?
> Second problem: __toString() is not correctly implemented in PHP <
> 5.2.0, so that on a test server with an older PHP version, the output is
> just "Object".
5.2.0 has been released in 2006! I'd rather not support software even
older than that.
> Here is, how I extended the phptal_tostring() and phptal_escape()
> functions to implement this workaround (3 small changes):
>
> http://pastie.org/2022716
Thanks. You bring very good point about implicit cast in implode(). I'll
fix that one.
--
regards, Kornel Lesiński
More information about the PHPTAL
mailing list