| David Zülke 2005-01-17, 8:59 pm |
| Jochem,
Someone posted a patch for this "problem" a couple of days ago on the
Developers list (this is where this discussion belongs to anyway, so I'm
crossposting there).
I totally agree with you, we need the possibility to use iterators. The
point is we need to find a clean way to implement this without breaking BC.
If we just do an is_object() check, we will break BC because you actually
CAN loop over objects using foreach, it will return the property values (and
their names as keys). We cannot remove the array casting either because some
people might rely on the fact that their int, string, whatever gets
converted to a one-value array and they can loop over them.
Messju should have the needed insight on this matter to propose a solution.
By the way, I'd also like to see some toString() magic in smarty that can be
enabled at runtime (as long as the PHP devs keep that feature removed :S).
But I guess this might be quite difficult to implement as well.
Cheers,
David
> -----Original Message-----
> From: Jochem Maas [mailto:jochem@iamjochem.com]
> Sent: Monday, January 17, 2005 4:34 PM
> To: Smarty general
> Subject: [SMARTY] php5 Iterators.
>
> 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
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
|