PHP | SplObjectStorage current() Function

The SplObjectStorage::current() function is an inbuilt function in PHP which is used get the current entry of storage.

Syntax:

object SplObjectStorage::current()

Parameters: This function does not accept any parameter.

Return Value: This function returns the object of the current storage.

Below programs illustrate the SplObjectStorage::current() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare an SplObjectStorage
$storage = new SplObjectStorage();
  
// Declare new object
$obj = new StdClass;
  
// Use attach() function to add object
$storage->attach($obj, "GeeksforGeeks");
  
$storage->rewind();
  
// Use current() function to get
// the current object
$object = $storage->current(); 
$data = $storage->getInfo();
  
var_dump($object);
var_dump($data);
?>

chevron_right


Output:

object(stdClass)#2 (0) {
}
string(13) "GeeksforGeeks"

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare an SplObjectStorage
$str = new SplObjectStorage();
  
// Declare new object
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$obj4 = new StdClass;
  
// Use attach() function to add object
$str->attach($obj1, "GeeksforGeeks");
$str->attach($obj2, "GFG");
$str->attach($obj3, "Geeks");
$str->attach($obj4, "PHP");
  
$str->rewind();
  
while($str->valid()) {
    $index = $str->key();
      
    // Use current() function to get
    // the current object
    $object = current($str); 
    $data = $str->getInfo();
  
    var_dump($object);
    var_dump($data);
    $str->next();
}
?>

chevron_right


Output:

bool(false)
string(13) "GeeksforGeeks"
bool(false)
string(3) "GFG"
bool(false)
string(5) "Geeks"
bool(false)
string(3) "PHP"

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.