Pages

Wednesday, March 26, 2014

The KeyedIterator interface




The KeyedIterator<Tk, Tv> interface 
(HHVM >= 2.5)
Introduction 
Interface for external iterators or objects that can be iterated themselves internally.
Interface synopsis 
KeyedIterator<Tk, Tv> extends KeyedTraversable<Tk, Tv> extends Iterator<Tv> {
/* Methods */
abstract public mixed KeyedIterator<Tv>::key ( void )
/* Inherited methods */
abstract public Tv Iterator<Tv>::current ( void )
abstract public mixed Iterator<Tv>::key ( void )
abstract public void Iterator<Tv>::next ( void )
abstract public void Iterator<Tv>::rewind ( void )
abstract public boolean Iterator<Tv>::valid ( void )
}
Examples 
Example #1 Basic usage
This example demonstrates how to iterate over a keyed iterator using foreach.
<?hh
class DuplicateStrIterator implements KeyedIterator<stringstring> {
  protected $it 0;

  public function current() {
    return $this->it;
  }

  public function key() {
    return chr(65 + ($this->it 5));
  }

  public function next() {
    $this->it++;
  }

  public function rewind() {
    $this->it 0;
  }

  public function valid() {
    return $this->it 10;
  }
}

$it = new DuplicateStrIterator;

foreach($it as $key => $value) {
  var_dump($key$value);
  echo "\n";
}
The above example will output something similar to:
string(1) "A"
int(0)

string(1) "B"
int(1)

string(1) "C"
int(2)

string(1) "D"
int(3)

string(1) "E"
int(4)

string(1) "A"
int(5)

string(1) "B"
int(6)

string(1) "C"
int(7)

string(1) "D"
int(8)

string(1) "E"
int(9)
Table of Contents 
·         KeyedIterator<Tv>::key — Return the key of the current element
      
KeyedIterator<Tv>::key
(HHVM >= 2.5)
KeyedIterator<Tv>::key  Return the key of the current element
Description 
abstract public mixed KeyedIterator<Tv>::key ( void )
Returns the key of the current element.
Parameters 
This function has no parameters.
Return Values 
Returns mixed on success, or NULL on failure.
Errors/Exceptions 
Issues E_NOTICE on failure.

No comments:

Post a Comment