The SplObjectStorage::rewind() function is an inbuilt function in PHP which is used to rewind the iterator to the first storage element.
Syntax:
void SplObjectStorage::rewind()
Parameters: This function does not accept any parameter.
Return Value: This function does not return any value.
Below programs illustrate the SplObjectStorage::rewind() function in PHP:
Program 1:
<?php
$str = new SplObjectStorage();
$obj = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$str ->attach( $obj , "GFG" );
$str ->attach( $obj2 , "Geeks" );
$str ->attach( $obj3 , "FORK JAVA" );
$str -> rewind ();
var_dump( $str ->getInfo());
$str ->next();
var_dump( $str ->getInfo());
$str -> rewind ();
var_dump( $str ->getInfo());
?>
|
Output:
string(3) "GFG"
string(5) "Geeks"
string(3) "GFG"
Program 2:
<?php
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$gfg = new SplObjectStorage();
$gfg [ $obj1 ] = "GFG" ;
$gfg [ $obj2 ] = "GeeksClasses" ;
$gfg [ $obj3 ] = "SUDO" ;
$gfg -> rewind ();
while ( $gfg ->valid()) {
var_dump( $gfg ->getInfo());
$gfg ->next();
}
?>
|
Output:
string(3) "GFG"
string(12) "GeeksClasses"
string(4) "SUDO"
Reference: https://www.php.net/manual/en/splobjectstorage.rewind.php