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

[email protected] (messju mohr)
Newsgroups php.smarty.dev
Message-ID <[email protected]>
On Tue, Feb 01, 2005 at 08:46:13AM -0800, boots wrote:
> --- messju mohr <[email protected]> wrote:
> > i know i'm a bit late in this discussion, but could we please "add
> > this feature" to the 2.6.x-tree of Smarty?
> > 
> > "add this feature" simply means omitting the (array)-cast for arrays
> > and objects. why is that such a big deal?
> >
> > sure $smarty.foreach.foo.total would be bogus on iterators, but as
> > someone already stated, people using iterators know that.
> > 
> > we not only enable iterators, but also SipleXML to be usable with
> > smarty with just adding one line.
>
> Will this be considered to bring iterator support into Smarty or are we
> simply not blocking it anymore?

i don't want to support any iterators. i just don't want objects to be
casted to arrays when passed to foreach.

> Because if we claim to support it, I
> suspect that while this patch will alleviate a first set of hurdles it
> will probably yield a new set of irritating questions and fumblings for
> those who hit up on the next unsupported and unchecked constraints.
> Iterators are a great feature but it really looks like they won't be
> solidly/completely implemented in PHP until 5.1. So I don't see the
> absolute rush. (For example, the iterator_to_array function (which is
> what I would think a cast to array *ought* do) should be available
> then)
> 
> Sigh. I'll go neutral on this -- people should have it if they want it,
> I guess. After all, it will be supported at some point.
> 
> xo boots
> 
> > Index: Smarty_Compiler.class.php
> > ===================================================================
> > RCS file: /repository/smarty/libs/Smarty_Compiler.class.php,v
> > retrieving revision 1.359
> > diff -u -r1.359 Smarty_Compiler.class.php
> > --- Smarty_Compiler.class.php   30 Jan 2005 21:54:39 -0000      1.359
> > +++ Smarty_Compiler.class.php   1 Feb 2005 09:16:35 -0000
> > @@ -1172,14 +1172,15 @@
> >          }
> > 
> >          $output = '<?php ';
> > +        $output .= "\$_from = $from; if (!is_array(\$_from) &&
> > !is_object(\$_from)) { settype(\$_from, 'array'); }";
> >          if (isset($name)) {
> >              $foreach_props = "\$this->_foreach[$name]";
> > -            $output .= "{$foreach_props} = array('total' =>
> > count(\$_from = (array)$from), 'iteration' => 0);\n";
> > +            $output .= "{$foreach_props} = array('total' =>
> > count(\$_from), 'iteration' => 0);\n";
> >              $output .= "if ({$foreach_props}['total'] > 0):\n";
> >              $output .= "    foreach (\$_from as
> > $key_part\$this->_tpl_vars['$item']):\n";
> >              $output .= "        {$foreach_props}['iteration']++;\n";
> >          } else {
> > -            $output .= "if (count(\$_from = (array)$from)):\n";
> > +            $output .= "if (count(\$_from)):\n";
> >              $output .= "    foreach (\$_from as
> > $key_part\$this->_tpl_vars['$item']):\n";
> >          }
> >          $output .= '?>';
> > 
> > 
> > there is no diversion here and no making things more complicated to
> > please php4 and 5 with the same compiler.
> > 
> > i really start getting pissed by people constantly stumbling about
> > the
> > array-cast in foreach and constantly begging for this "feature".
> > 
> > could we please change that?
> > 
> > greetings
> > messju
> > 
> > 
> > On Tue, Jan 25, 2005 at 01:02:05PM -0800, boots wrote:
> > > The more I look at the iterator implementation in PHP5 (especially
> > as
> > > it is in the 5.0.x builds) the more I think support for it should
> > not
> > > be included into any 2.x series of Smarty. I think that a rewrite
> > of
> > > Smarty specificially designed for PHP5 would be the "right" place
> > to
> > > implement it. It still seems like awhile before that happens so
> > perhaps
> > > talking about proposed feature sets, design and usage goals would
> > be
> > > appropriate to get that ball rolling.
> > >
> > > As for 2.x support of iterators, I think it should be left out of
> > the
> > > core and provided either as an addin or in a PHP5 only class that
> > > extends the base PHP4. Doing so can ease the transition as there
> > are
> > > already requests for Smarty versions that obey E_STRICT and
> > supplying
> > > an extended Smarty class intended for PHP5 is one way of supporting
> > > that and other features that would be oddball in the PHP4
> > > implementation.
> > > 
> > > xo boots
> > > 
> > > --- David Z?lke <[email protected]> wrote:
> > > > We _could_ count the number of properties in the array using
> > > > count((array)$foo); I can't see any way of resolving item count,
> > > > position,
> > > > first/last etc from an iterator. People who're using iterators
> > are
> > > > most
> > > > likely aware of this and can work around it or just live with
> > that
> > > > "problem".
> > > > 
> > > > You're right, it's time to tackle this one, but I also think the
> > time
> > > > might
> > > > have come to do a complete PHP5 rewrite of Smarty. I'd be happy
> > to
> > > > help.
> > > > 
> > > > 	David
> > > > 
> > > > 
> > > > > -----Original Message-----
> > > > > From: boots [mailto:[email protected]]
> > > > > Sent: Monday, January 24, 2005 11:19 AM
> > > > > To: David Z?lke; 'Mark Rogers'; [email protected]
> > > > > Cc: 'boots'
> > > > > Subject: RE: [SMARTY-DEV] RE: [SMARTY] php5 Iterators.
> > > > > 
> > > > > --- 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.