ArrayObject::setIteratorClass


Sets the iterator classname for the ArrayObject.



<?php

void 
public ArrayObject::setIteratorClass string $iterator_class )


where,

$iterator_class The class name of the array iterator 
                           to 
use when iterating over this object

?>

 $iterator_class 


The classname of the array iterator to use when iterating over this object.



  1 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>';

?>

 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)
}
}
}
}
}


  2 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>';

?>

 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"
}
}
}


  3 EXERCISE   

<?php

class nExample 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 nExample;

print_r($obj03);

$arrayobj = new ArrayObject($obj03);

$arrayobj->setIteratorClass('nExample');

$It $arrayobj->getIterator();

echo 
'<pre>';
var_dump($It);
echo 
'</pre>';

?>

 RESULT   

nExample Object ( [pubc] => Alea Jacta Est [priv:nExample:private] => Maxima debetur puero reverentia [prot:protected] => Ad augusta per angusta [storage:ArrayIterator:private] => Array ( ) )

object(nExample)#3 (4) {
["pubc"]=>
string(14) "Alea Jacta Est"
["priv":"nExample":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(nExample)#1 (4) {
["pubc"]=>
string(14) "Alea Jacta Est"
["priv":"nExample":private]=>
string(31) "Maxima debetur puero reverentia"
["prot":protected]=>
string(22) "Ad augusta per angusta"
["storage":"ArrayIterator":private]=>
array(0) {
}
}
}
}