Hi Kornel,

Am 05.06.2011 18:16, schrieb Kornel Lesiński:
On Sun, 05 Jun 2011 15:58:06 +0100, Manu Robledo <moli...@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?

I think yes, there is. We are using a widget-based system, where small snippets of HTML are represented by single objects. These are passed on to the templating system (in this case PHPTAL). They can generate their output code by an explicit call to their output() method, but __toString() will do the same.

It would be possible to call output() first and pass the results to PHPTAL, but this way also widgets, which are not visible in the current template (maybe because of some tal:condition would have to be rendered.

Now in a good software design each component should be able to throw exceptions and pass them over to the caller. So should a widget be able to throw exceptions, if anything went wrong during rendering.

Without this possibility, I would have to use a try/catch inside __toString(), log any exception and return something like "an error occured. see the log for details.".

I see, that this is not the most important thing, but since the workaround is so easy, I´m still in favour of including it into PHPTAL.

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.

You are right. But same here: If the solution is that simple, why not build it in? Unfortunately there are still servers out there with such old php versions.

Regards,
Manu

_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

Reply via email to