<?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
}
?>
MAGICAL CONSTANTS | |
NAME | MEANING |
__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 |
<?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>');
}
?>
<?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 (1 < $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>";
}
?>