Re: Are PHP5 features worth it?
| From: | Roman Neuhauser | Date: | Wed, 20 Dec 2006 13:54:10 +0000 |
| Subject: | Re: Are PHP5 features worth it? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-246072@lists.php.net to get a copy of this message | ||
# robert@interjinn.com / 2006-12-20 06:05:29 -0500:
> On Wed, 2006-12-20 at 11:49 +0100, Jochem Maas wrote:
> > >
> > > How about iterators? You can have objects that look like arrays yet they take
> > > much less memory:
> > >
> > > $rs = $db->query($select); # query the db
> > > foreach ($rs as $row) { # fetch the row
> > > whatever($row);
> > > }
> >
> > ah yes - good catch, I do like iterators for keeping code nice and tight.
>
> I don't what you guys did in PHP4, but I do the following:
>
> <?php
>
> if( $db->query( $query ) )
> {
> while( ($row = $db->fetchRow()) )
> {
> whatever( $row );
> }
> }
>
> ?>
>
> I fail to see the need for an iterator. But, I'll grant you, proper
> destructor support is nice.
There's no need just as there's no need for the while. Both are
conveniences. next() is more convenient than the because it's generic.
A single implementation can operate on an array, a DirectoryIterator,
SPLFileObject or MyDBResultSet.
Take a look at the STL[1] to get an idea what a set of generic
interfaces can do for you. Short answer: wonders!
Oh, and BTW, I do remember those days of painfully boring
if (!operation()) {
return some_error_indication;
}
whatever();
What we do in PHP 5 is use airbags and safety belts:
operation(); // throws on errors, taken care of by the caller
whatever();
BTW, fetchRow() is (IMO) better than next() *if used in while()* because
the code reads more naturally.
foreach ($rs as $row)
IMO wins over
while($row = $rs->fetchRow())
although this would win my vote:
foreach ($row in $rs) // pseudocode
[1] http://www.sgi.com/tech/stl/
--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991