Friday, October 3, 2025
HomeLanguagesPHP | ArrayObject setIteratorClass() Function

PHP | ArrayObject setIteratorClass() Function

The ArrayObject::setIteratorClass() function is an inbuilt function in PHP which is used to set the iterator classname for the ArrayObject.

Syntax:

void ArrayObject::setIteratorClass( string $iterator_class )

Parameters: This function accepts single parameter $iterator_class which holds the class name of the array iterator. It is used when iterating it over this object.

Return Value: This function does not return any value.

Below programs illustrate the ArrayObject::setIteratorClass() function in PHP:

Program 1:




<?php 
// PHP program to illustrate the 
// ArrayObject::setIteratorClass() function 
  
// Custom ArrayIterator (inherits from ArrayIterator)
class iteratorClass extends ArrayIterator {
  
}
  
// Create array object 
$arrayObjectElement = new ArrayObject(
    array('Geeks', 'for', 'Geeks')
); 
  
// Use ArrayObject::setIteratorClass() function
// to set the iterator classname for the ArrayObject
$arrayObjectElement->setIteratorClass('iteratorClass');
  
print_r($arrayObjectElement->getIterator());
  
?>


Output:

iteratorClass Object
(
    [storage:ArrayIterator:private] => ArrayObject Object
        (
            [storage:ArrayObject:private] => Array
                (
                    [0] => Geeks
                    [1] => for
                    [2] => Geeks
                )

        )

)

Program 2:




<?php 
// PHP program to illustrate the 
// ArrayObject::setIteratorClass() function 
  
// Custom ArrayIterator (inherits from ArrayIterator)
class iteratorClass extends ArrayIterator {
  
}
  
// Declare an associative array
$arr = array(
    "a" => "Welcome",
    "b" => "to", 
    "d" => "neveropen"
); 
  
// Create array object 
$arrayObjectElement = new ArrayObject($arr); 
  
// Use ArrayObject::setIteratorClass() function
// to set the iterator classname for the ArrayObject
$arrayObjectElement->setIteratorClass('iteratorClass');
  
print_r($arrayObjectElement->getIterator());
  
?>


Output:

iteratorClass Object
(
    [storage:ArrayIterator:private] => ArrayObject Object
        (
            [storage:ArrayObject:private] => Array
                (
                    [a] => Welcome
                    [b] => to
                    [d] => neveropen
                )

        )

)

Reference: https://www.php.net/manual/en/arrayobject.setiteratorclass.php

RELATED ARTICLES

Most Popular

Dominic
32332 POSTS0 COMMENTS
Milvus
85 POSTS0 COMMENTS
Nango Kala
6703 POSTS0 COMMENTS
Nicole Veronica
11868 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11929 POSTS0 COMMENTS
Shaida Kate Naidoo
6818 POSTS0 COMMENTS
Ted Musemwa
7080 POSTS0 COMMENTS
Thapelo Manthata
6775 POSTS0 COMMENTS
Umr Jansen
6776 POSTS0 COMMENTS