Thursday, October 23, 2025
HomeLanguagesPHP SplObjectStorage offsetGet() Function

PHP SplObjectStorage offsetGet() Function

The SplObjectStorage::offsetGet() function is an inbuilt function in PHP that is used to get the data associated with the object.

Syntax: 

object SplObjectStorage::offsetGet($obj)

Parameters: This function accepts a single parameter $obj which specifies the object to be fetched.

Return Value: This function returns the data previously associated with the object in the storage.

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

Program 1: 

php




<?php
  
// Create an empty SplObjectStorage
$str = new SplObjectStorage;
$obj = new StdClass;
  
// Attach $obj to $str 
$str->attach($obj, "neveropen");
  
// Print Result
var_dump($str->offsetGet($obj)); 
?>


Output: 

string(13) "neveropen"

 

Program 2: 

PHP




<?php
  
// Create an Empty SplObjectStorage
$str = new SplObjectStorage();
   
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$obj4 = new StdClass;
  
// Attach objects
$str->attach($obj1, "neveropen");
$str->attach($obj2, "GFG");
$str->attach($obj3);
$str->attach($obj4, "Hello GFG");
  
// Print result
var_dump($str->offsetGet($obj1)); 
var_dump($str->offsetGet($obj2)); 
var_dump($str->offsetGet($obj4)); 
var_dump($str->offsetGet($obj3)); 
  
?>


Output

string(13) "neveropen"
string(3) "GFG"
string(9) "Hello GFG"
NULL

Reference: https://www.php.net/manual/en/splobjectstorage.offsetget.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

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS