php5 Iterators.
| From: | Jochem Maas | Date: | Mon, 17 Jan 2005 15:33:54 +0000 |
| Subject: | php5 Iterators. | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-12164@lists.php.net to get a copy of this message | ||
hi List,
I have PHP5 objects which implement the Iterator interface, with regard to using the foreach smarty tag with these objects the following 2 lines from Smarty_Compiler.class.php breaks on objects that implement the iterator interface - I can't remember if I hacked my previous version of Smarty or whether this worked before (yesterday I had 2.6.0 today I have 2.6.5 - I keep a copy in my personal CVS but I screwed it up so I can't check the old revision :-( ):
[Smarty_Compiler.class.php: lines 1167 and 1176 - Smarty version 2.6.5]
$output .= "{$foreach_props}['total'] = count(\$_from = (array)$from);\n";
...
$output .= "if (count(\$_from = (array)$from)):\n";
anyway my question is could a fix be applied to allow iterator objects to go thru the foreach stuff unscathed; the simple solution would be to remove the array typecast, although this may allow errors to creep in and remove sideeffects other people rely on, actually that still leaves a problem with count() - which will only work if the object implements the Countable interface (which is only available in the most bleeding edge versions of PHP5, where as the Iterator interface has been around for ages.) - my personal hack for these two lines is as follows (all my objects that implement Iterator also implement IteratorExtension, a personal interface which does the same as Countable, only I did't have the Countable interface a year ago - actually I still don't have it available!):
$output .= "{$foreach_props}['total'] = (is_object(\$_from = $from) && \$_from instanceof IteratorExtension) ? \$_from->count(): count(\$_from = (array)$from);\n";
...
$output .= "if ((is_object(\$_from = $from) && \$_from instanceof IteratorExtension && \$_from->count()) || count(\$_from = (array)$from)):\n";
Obviously my hacks are not correct as such because they will break smarty on PHP4. besides which Iterator objects must implement a custom interface (not exactly what you want in Smarty, me thinks).
Any idea as to whether a change to accomodate this might occur? I'm happy to help think the problem thru (same goes for any issues related to problems with PHP5 syntax - e.g. dereferenced syntax)
kind regards,
Jochem