Saturday, June 13, 2026
HomeLanguagesPHP SplObjectStorage rewind() Function

PHP SplObjectStorage rewind() Function

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

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS