ArrayObject::getIteratorClass
Gets the iterator classname for the ArrayObject.
<?php
str public ArrayObject::getIteratorClass ( void )
?>
$iterator_class
The classname of the array iterator to use when iterating over this object.
EXERCISE
<?php
class MyArrayIterator extends ArrayIterator {
}
$RGBcolors = [ "red" => 0xFF0000,
"green" => 0x00FF00,
"blue" => 0x0000FF ];
$CMYKcolors = [ "cyan" => 0x00FFFF,
"magenta" => 0xFF00FF,
"yellow" => 0xFFFF00,
"black" => 0x000000 ];
$colors = new ArrayObject($RGBcolors);
$colors->append($CMYKcolors);
$colorsArrayObject = new ArrayObject($colors);
$colorsArrayObject->setIteratorClass('MyArrayIterator');
$RGB_CMYK = $colorsArrayObject->getIterator();
echo '<pre>';
var_dump($RGB_CMYK);
echo '</pre>';
$classname = $colorsArrayObject->getIteratorClass();
var_dump($classname);
?>
RESULT
object(MyArrayIterator)#3 (1) {
["storage":"ArrayIterator":private]=>
object(ArrayObject)#2 (1) {
["storage":"ArrayObject":private]=>
object(ArrayObject)#1 (1) {
["storage":"ArrayObject":private]=>
array(4) {
["red"]=>
int(16711680)
["green"]=>
int(65280)
["blue"]=>
int(255)
[0]=>
array(4) {
["cyan"]=>
int(65535)
["magenta"]=>
int(16711935)
["yellow"]=>
int(16776960)
["black"]=>
int(0)
}
}
}
}
}
The Iterator class name for this ArrayObject is given by:
string(15) "MyArrayIterator"
EXERCISE
<?php
class Example extends ArrayIterator {
public $public = 'public variable';
private $priv = 'private variable';
protected $prot = 'protected variable';
}
$arrayobj = new ArrayObject(new Example());
$arrayobj = new ArrayObject(['Alea jact est',
'Maxima debetur puero reverentia',
'Ad augusta per angusta']);
echo 'In this ArrayObject, there are ' . $arrayobj->count() . ' public properties.';;
$arrayobj->setIteratorClass('Example');
$It = $arrayobj->getIterator();
echo '<pre>';
var_dump($It);
echo '</pre>';
$Itname = $arrayobj->getIteratorClass();
echo $Itname;
?>
RESULT
In this ArrayObject, there are 3 public properties.
object(Example)#1 (4) {
["public"]=>
string(15) "public variable"
["priv":"Example":private]=>
string(16) "private variable"
["prot":protected]=>
string(18) "protected variable"
["storage":"ArrayIterator":private]=>
object(ArrayObject)#3 (1) {
["storage":"ArrayObject":private]=>
array(3) {
[0]=>
string(13) "Alea jact est"
[1]=>
string(31) "Maxima debetur puero reverentia"
[2]=>
string(22) "Ad augusta per angusta"
}
}
}
The Iterator class name for this ArrayObject is:
Example
EXERCISE
<?php
class newExample extends ArrayIterator {
public string $pubc = 'Alea Jacta Est';
private string $priv = 'Maxima debetur puero reverentia';
protected string $prot = 'Ad augusta per angusta';
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This type declarations is supported,
only, as of PHP 7.4.0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$obj03 = new newExample;
print_r($obj03);
$arrayobj = new ArrayObject($obj03);
$arrayobj->setIteratorClass('newExample');
$Itr = $arrayobj->getIterator();
echo '<pre>';
var_dump($Itr);
echo '</pre>';
$Itrcn = $arrayobj->getIteratorClass();
echo $Itrcn;
?>
RESULT
newExample Object ( [pubc] => Alea Jacta Est [priv:newExample:private] => Maxima debetur puero reverentia [prot:protected] => Ad augusta per angusta [storage:ArrayIterator:private] => Array ( ) )
object(newExample)#3 (4) {
["pubc"]=>
string(14) "Alea Jacta Est"
["priv":"newExample":private]=>
string(31) "Maxima debetur puero reverentia"
["prot":protected]=>
string(22) "Ad augusta per angusta"
["storage":"ArrayIterator":private]=>
object(ArrayObject)#2 (1) {
["storage":"ArrayObject":private]=>
object(newExample)#1 (4) {
["pubc"]=>
string(14) "Alea Jacta Est"
["priv":"newExample":private]=>
string(31) "Maxima debetur puero reverentia"
["prot":protected]=>
string(22) "Ad augusta per angusta"
["storage":"ArrayIterator":private]=>
array(0) {
}
}
}
}
The Iterator class name for this ArrayObject is:
newExample