RE: [SMARTY-DEV] RE: [SMARTY] php5 Iterators.

[email protected] (boots)
Newsgroups php.smarty.dev
Message-ID <[email protected]>
--- David Zülke <[email protected]> wrote:
> Couldn't we just change the array casting so it is only performed if
> the variable is _not_ an object?
> 
> foreach($object as $key => $ value) {}
> is the same as
> foreach((array)$object as $key => $ value) {}
> 
> Both will loop over the properties of an object.
> 
> It will also work in PHP5. If there's an iterator defined, PHP will
> use it, if not, it will loop over the properties just like in the
good 
> old times.

This reminds me of some correspondance from last year -- back in July
messju raised this issue with Monte and I and at the end of it the
following was proposed:

$_from =& $this->_tpl_vars['list']; // by ref for php4 BC
if ( count($_from) && ( is_array($_from) || is_object($_from) ) ):
    foreach ($_from as $this->_tpl_vars['cell']):
    ...
    endforeach;
else:
   // err
endif;
unset($_from);

At the time, messju was against the =& so as to not raise the
possiblity of subtle bugs. Otherwise, we were all in agreement that it
was a reasonable approach. As I still haven't really delved into PHP5,
I am in no position to understand the minute issues that might arise.
My understanding is that there is a difficulty with count and the need
for certain interfaces to be implemented for this to work properly for
objects. For example, Bok proposed a different solution
http://news.php.net/php.smarty.dev/2510 but messju pointed out some of
the issues that it raised: http://news.php.net/php.smarty.dev/2515 

My only concern is that too much PHP5 specific code to check for
correctness gets inserted into the foreach procedure thereby hampering
PHP4 performance in any way. Otherwise, it seems like the time is
approaching where this needs to be addressed.

xo boots
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.