Pages

Wednesday, March 26, 2014

The Iterator interface



The Iterator<Tv> interface 
(HHVM >= 2.5)
Introduction 
Interface for external iterators or objects that can be iterated themselves internally.
Interface synopsis 
Iterator<Tv> extends Traversable<Tv> {
/* Methods */
abstract public Tv current ( void )
abstract public mixed key ( void )
abstract public void next ( void )
abstract public void rewind ( void )
abstract public boolean valid ( void )
}
Examples 
Example #1 Basic usage
This example demonstrates in which order methods are called when using foreach with an iterator.
<?hh
class myIterator<Tv> implements Iterator<Tv> {
  private $position 0;
  private $array = array(
    "firstelement",
    "secondelement",
    "lastelement",
  );  

  public function __construct() {
    $this->position 0;
  }

  function rewind(): void {
    var_dump(__METHOD__);
    $this->position 0;
  }

  function current(): Tv {
    var_dump(__METHOD__);
    return $this->array[$this->position];
  }

  function next(): void {
    var_dump(__METHOD__);
    ++$this->position;
  }

  function valid(): bool {
    var_dump(__METHOD__);
    return isset($this->array[$this->position]);
  }
}

$it = new myIterator;

foreach($it as $value) {
  var_dump($value);
  echo "\n";
}
The above example will output something similar to:
string(18) "myIterator::rewind"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
string(12) "firstelement"

string(16) "myIterator::next"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
string(13) "secondelement"

string(16) "myIterator::next"
string(17) "myIterator::valid"
string(19) "myIterator::current"
string(15) "myIterator::key"
string(11) "lastelement"

string(16) "myIterator::next"
string(17) "myIterator::valid"
Table of Contents 
·         Iterator<Tv>::current — Return the current element
·         Iterator<Tv>::key — Return the key of the current element
·         Iterator<Tv>::next — Move forward to next element
·         Iterator<Tv>::rewind — Rewind the Iterator to the first element
·         Iterator<Tv>::valid — Checks if current position is valid

Iterator<Tv>::current
(HHVM >= 2.5)
Iterator<Tv>::current  Return the current element
Description 
abstract public Tv Iterator<Tv>::current ( void )
Returns the current element.
Parameters 
This function has no parameters.
Return Values 
Can return any type.

Iterator<Tv>::key
(HHVM >= 2.5)
Iterator<Tv>::key  Return the key of the current element
Description 
abstract public mixed Iterator<Tv>::key ( void )
Returns NULL. Use KeyedIterator<Tk, Tv> for key-based iteration.
Parameters 
This function has no parameters.
Return Values 
Returns NULL
Errors/Exceptions 
Issues E_NOTICE on failure.

Iterator<Tv>::next
(HHVM >= 2.5)
Iterator<Tv>::next  Move forward to next element
Description 
abstract public void Iterator<Tv>::next ( void )
Moves the current position to the next element.
Note:
This method is called after each foreach loop.
Parameters 
This function has no parameters.
Return Values 
Any returned value is ignored.

Iterator<Tv>::rewind
(HHVM >= 2.5)
Iterator<Tv>::rewind  Rewind the Iterator to the first element
Description 
abstract public void Iterator<Tv>::rewind ( void )
Rewinds back to the first element of the Iterator.
Note:
This is the first method called when starting a foreach loop. It will not be executed after foreach loops.
Parameters 
This function has no parameters.
Return Values 
Any returned value is ignored.

erator<Tv>::valid
(HHVM >= 2.5)
Iterator<Tv>::valid  Checks if current position is valid
Description 
abstract public boolean Iterator<Tv>::valid ( void )
This method is called after Iterator::rewind() and Iterator::next() to check if the current position is valid.
Parameters 
This function has no parameters.
Return Values 
The return value will be casted to boolean and then evaluated. Returns TRUE on success or FALSE on failure.

No comments:

Post a Comment