Re: [PHPTAL] Usage of __toString magic method

2011-06-07 Thread Kornel Lesiński

On Mon, 06 Jun 2011 07:39:25 +0100, Manu Robledo  wrote:

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.


You could also use ${widget/output} instead of ${widget}.

Lack of exceptions in __toString() is an annoyance, but that's PHP's rule.  
Even if it's worked around in PHPTAL, will you want workaround everywhere?  
Will you change every echo and cast to an explicit call? Then you don't  
really use __toString — why implement it at all?


If anything, I'd rather add _phptal_toString() or such, that doesn't  
forbid exceptions by design. Otherwise you have magic method that you  
don't want called magically…


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.


Let's give them another reason to update seriously outdated—and likely  
insecure—software.


--
regards, Kornel Lesiński

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


Re: [PHPTAL] Usage of __toString magic method

2011-06-05 Thread Manu Robledo

Hi Kornel,

Am 05.06.2011 18:16, schrieb Kornel Lesiński:

On Sun, 05 Jun 2011 15:58:06 +0100, Manu Robledo  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:




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


Re: [PHPTAL] Usage of __toString magic method

2011-06-05 Thread Kornel Lesiński

On Sun, 05 Jun 2011 15:58:06 +0100, Manu Robledo  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:



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

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