For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > January 2005 > php5 Iterators.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author php5 Iterators.
Jochem Maas

2005-01-17, 4:00 pm

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com