SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IteratorIterator current rewind next

    Does anyone have any idea why rewind() or next() needs to be called before current() will return a value?

    PHP Code:
    $it = new IteratorIterator(new ArrayIterator(array(1,2)));
    var_dump($it->valid()); //false
    var_dump($it->current()); //null 
    PHP Code:
    $it = new IteratorIterator(new ArrayIterator(array(1,2)));
    $it->rewind();
    var_dump($it->valid()); //true
    var_dump($it->current()); //1 
    Yet this doesn't need rewind() or next()
    PHP Code:
    $it = new ArrayIterator(array(1,2));
    var_dump($it->valid()); //true
    var_dump($it->current()); //1 
    I'm curious if/why it was designed this way, or if it's a bug. Using php 5.3 btw

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, this similar bug report was marked bogus http://bugs.php.net/bug.php?id=42279

    But I still wonder why it would be designed like this. I personally can't think of any good reason.

    And calling next() goes to the second element, as if it was already on the first. But I guess that's "undefined behavior" anyway.
    PHP Code:
    $it = new IteratorIterator(new ArrayIterator(array(1,2))); 
    $it->next();
    var_dump($it->valid()); //true
    var_dump($it->current()); //2 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •