Iterator interface


Interface for external iterators or objects that can be iterated themselves internally.



<?php

 Iterator 
extends Traversable {
/* Methods */
abstract public current void ) : mixed
abstract public key void ) : scalar
abstract public next void ) : void
abstract public rewind void ) : void
abstract public valid void ) : bool
}

?>

 ATTENTION 


PHP already provides a number of iterators for many day to day tasks.

This next example demonstrates in which order methods are called when using foreach with an iterator.



 Magical Constants


computer apg

To allow to determine the line number, the full path and filename of the file with symlinks resolved, the directory of the file.

If used inside an include, the directory of the included file is returned, the function name, the class name, the trait name, the class method and the namespace.

PHP provides a large number of predefined constants to any script which it runs.

Many of these constants, however, are created by various extensions, and will only be present when those extensions are available, either via dynamic loading or because they have been compiled in.

These special constants are case-insensitive.

All these Magical Constants are resolved at compile time, unlike Regular Constants, which are resolved at runtime.


MAGICAL CONSTANTS
NAMEMEANING
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file with symlinks resolved.
If used inside an include, the name of the included file is returned.
__DIR__ The directory of the file.
If used inside an include, the directory of the included file is returned.
This is equivalent to dirname(__FILE__).
This directory name does not have a trailing slash unless it is the root directory.
__FUNCTION__ The function name, or {closure} for anonymous functions.
__CLASS__ The class name.
The class name includes the namespace it was declared in (e.g. Foo\Bar).
Note that as of PHP 5.4 __CLASS__ works also in traits.
When used in a trait method, __CLASS__ is the name of the class the trait is used in.
__TRAIT__ The trait name.
The trait name includes the namespace it was declared in (e.g. Foo\Bar).
__METHOD__ The class method name.
__NAMESPACE__ The name of the current namespace.
ClassName::class The fully qualified class name.
ed48

  1 EXERCISE   

<?php

class AIterator implements Iterator {
    private 
$position 0;
    private 
$array = [ 'South America''Central America''North America' ];  

    public function 
__construct() {
        
$this->position 0;
        
writemsg('<br>');
    }

    public function 
rewind() {
        
writemsg('REWIND TO INITIAL KEY<br>');
        
var_dump(__METHOD__);
        
writemsg('<br>');
        
$this->position 0;
        
writemsg('<br>');
    }

    public function 
current() {
        
writemsg('<br>CURRENT KEY<br>');
        
var_dump(__METHOD__);
        
writemsg('<br>');
        return 
$this->array[$this->position];
        
writemsg('<br>');
    }

    public function 
key() {
        
var_dump(__METHOD__);
        
writemsg('<br>');
        return 
$this->position;
        
writemsg('<br>');
    }

    public function 
next() {
        
writemsg('NEXT KEY<br>');
        
var_dump(__METHOD__);
        
writemsg('<br>');
        ++
$this->position;
        
writemsg('<br>');
    }

    public function 
valid() {
        
var_dump(__METHOD__);
        
writemsg('<br>');
        return isset(
$this->array[$this->position]);
        
writemsg('<br>');
    }
}

  function 
writemsg($msg) {
    echo 
$msg;
  }

$iterator = new AIterator;

foreach(
$iterator as $key => $value) {
    
writemsg('<br>');
    
var_dump($key$value);
    
writemsg('<br>');
}

?>

 RESULT   

REWIND TO INITIAL KEY
string(18) "AIterator::rewind"

string(17) "AIterator::valid"

CURRENT KEY
string(19) "AIterator::current"
string(15) "AIterator::key"

int(0) string(13) "South America"

NEXT KEY
string(16) "AIterator::next"

string(17) "AIterator::valid"

CURRENT KEY
string(19) "AIterator::current"
string(15) "AIterator::key"

int(1) string(15) "Central America"

NEXT KEY
string(16) "AIterator::next"

string(17) "AIterator::valid"

CURRENT KEY
string(19) "AIterator::current"

string(15) "AIterator::key"

int(2) string(13) "North America"

NEXT KEY
string(16) "AIterator::next"

string(17) "AIterator::valid"


  2 EXERCISE   

<?php

class Country {
    private 
$country;
    private 
$president;
    function 
__construct($president_in$country_in) {
      
$this->country $country_in;
      
$this->president  $president_in;
    }
    function 
getCcontry() {return $this->country;}
    function 
getTpresident() {return $this->president;}
    function 
getCcontryAndTpresident() {
      return 
$this->getTpresident() . ' by ' $this->getCcontry();
    }
}

class 
CountryList {
    private 
$countrys = array();
    private 
$countryCount 0;
    public function 
__construct() {
    }
    public function 
getCountryCount() {
      return 
$this->countryCount;
    }
    private function 
setCountryCount($newCount) {
      
$this->countryCount $newCount;
    }
    public function 
getCountry($countryNumberToGet) {
      if ( (
is_numeric($countryNumberToGet)) && 
           (
$countryNumberToGet <= $this->getCountryCount())) {
           return 
$this->countrys[$countryNumberToGet];
         } else {
           return 
NULL;
         }
    }
    public function 
addCountry(Country $country_in) {
      
$this->setCountryCount($this->getCountryCount() + 1);
      
$this->countrys[$this->getCountryCount()] = $country_in;
      return 
$this->getCountryCount();
    }
    public function 
removeCountry(Country $country_in) {
      
$counter 0;
      while (++
$counter <= $this->getCountryCount()) {
        if (
$country_in->getCcontryAndTpresident() == 
          
$this->countrys[$counter]->getCcontryAndTpresident())
          {
            for (
$x $counter$x $this->getCountryCount(); $x++) {
              
$this->countrys[$x] = $this->countrys[$x 1];
          }
          
$this->setCountryCount($this->getCountryCount() - 1);
        }
      }
      return 
$this->getCountryCount();
    }
}

class 
CountryListIterator {
    protected 
$countryList;
    protected 
$currentCountry 0;

    public function 
__construct(CountryList $countryList_in) {
      
$this->countryList $countryList_in;
    }
    public function 
getCurrentCountry() {
      if ((
$this->currentCountry 0) && 
          (
$this->countryList->getCountryCount() >= $this->currentCountry)) {
        return 
$this->countryList->getCountry($this->currentCountry);
      }
    }
    public function 
getNextCountry() {
      if (
$this->hasNextCountry()) {
        return 
$this->countryList->getCountry(++$this->currentCountry);
      } else {
        return 
NULL;
      }
    }
    public function 
hasNextCountry() {
      if (
$this->countryList->getCountryCount() > $this->currentCountry) {
        return 
TRUE;
      } else {
        return 
FALSE;
      }
    }
}

class 
CountryListReverseIterator extends CountryListIterator {
    public function 
__construct(CountryList $countryList_in) {
      
$this->countryList $countryList_in;
      
$this->currentCountry $this->countryList->getCountryCount() + 1;
    }
    public function 
getNextCountry() {
      if (
$this->hasNextCountry()) {
        return 
$this->countryList->getCountry(--$this->currentCountry);
      } else {
        return 
NULL;
      }
    }
    public function 
hasNextCountry() {
      if (
$this->currentCountry) {
        return 
TRUE;
      } else {
        return 
FALSE;
      }
    }
}


  
writeln('BEGIN TESTING ITERATOR PATTERN');
  
writeln('');
 
  
$firstCountry = new Country('Spain''Pedro Sánchez');
  
$secondCountry = new Country('Germany''Frank-Walter Steinmeier');
  
$thirdCountry = new Country('Portugal''Marcelo Rebelo de Souza');

  
$countrys = new CountryList();
  
$countrys->addCountry($firstCountry);
  
$countrys->addCountry($secondCountry);
  
$countrys->addCountry($thirdCountry);
 
  
writeln('Testing the Iterator');
 
  
$countrysIterator = new CountryListIterator($countrys);

  while (
$countrysIterator->hasNextCountry()) {
    
$country $countrysIterator->getNextCountry();
    
writeln('getting next country with iterator:');
    
writeln($country->getCcontryAndTpresident());
    
writeln('');
  }
 
  
$country $countrysIterator->getCurrentCountry();
  
writeln('getting current country with iterator:');
  
writeln($country->getCcontryAndTpresident());
  
writeln('');  

  
writeln('Testing the Reverse Iterator');

  
$countrysReverseIterator = new CountryListReverseIterator($countrys);

  while (
$countrysReverseIterator->hasNextCountry()) {
    
$country $countrysReverseIterator->getNextCountry();
    
writeln('getting next country with reverse iterator:');
    
writeln($country->getCcontryAndTpresident());
    
writeln('');
  }
 
  
$country $countrysReverseIterator->getCurrentCountry();
  
writeln('getting current country with reverse iterator:');
  
writeln($country->getCcontryAndTpresident());
  
writeln('');

  
writeln('END TESTING ITERATOR PATTERN');
 
  function 
writeln($line_in) {
    echo 
$line_in "<br>";
  }

?>

 RESULT   

BEGIN TESTING ITERATOR PATTERN

Testing the Iterator
getting next country with iterator:
Spain by Pedro Sánchez

getting next country with iterator:
Germany by Frank-Walter Steinmeier

getting next country with iterator:
Portugal by Marcelo Rebelo de Souza

getting current country with iterator:
Portugal by Marcelo Rebelo de Souza

Testing the Reverse Iterator

getting next country with reverse iterator:
Portugal by Marcelo Rebelo de Souza

getting next country with reverse iterator:
Germany by Frank-Walter Steinmeier

getting next country with reverse iterator:
Spain by Pedro Sánchez

getting current country with reverse iterator:
Spain by Pedro Sánchez

END TESTING ITERATOR PATTERN