Re: Are PHP5 features worth it?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



# robert@xxxxxxxxxxxxx / 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux