Sunday, November 17, 2024
Google search engine
HomeLanguagesPHP SplObjectStorage current() Function

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:




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


Output:

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

Program 2:




<?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, "neveropen");
$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();
}
?>


Output:

bool(false)
string(13) "neveropen"
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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments