Open In App

PHP SplObjectStorage rewind() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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
  
// Create an empty SplObjectStorage
$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");
  
// Using rewind function
$str->rewind();
  
// Get current data 
var_dump($str->getInfo());
  
// Move on to next object
$str->next();
  
// Get current data 
var_dump($str->getInfo());
  
// Again using rewind function
$str->rewind();
  
// Get current data 
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";
  
// Using rewind function
$gfg->rewind();
  
while($gfg->valid()) {
    var_dump($gfg->getInfo());
      
    // Moving to next element
    $gfg->next();
}
?>


Output:

string(3) "GFG"
string(12) "GeeksClasses"
string(4) "SUDO"

Reference: https://www.php.net/manual/en/splobjectstorage.rewind.php



Last Updated : 23 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads