PHP SplObjectStorage offsetGet() Function
Last Updated :
23 Jun, 2023
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
$str = new SplObjectStorage;
$obj = new StdClass;
$str ->attach( $obj , "GeeksforGeeks" );
var_dump( $str ->offsetGet( $obj ));
?>
|
Output:
string(13) "GeeksforGeeks"
Program 2:
PHP
<?php
$str = new SplObjectStorage();
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$obj4 = new StdClass;
$str ->attach( $obj1 , "GeeksforGeeks" );
$str ->attach( $obj2 , "GFG" );
$str ->attach( $obj3 );
$str ->attach( $obj4 , "Hello GFG" );
var_dump( $str ->offsetGet( $obj1 ));
var_dump( $str ->offsetGet( $obj2 ));
var_dump( $str ->offsetGet( $obj4 ));
var_dump( $str ->offsetGet( $obj3 ));
?>
|
Output
string(13) "GeeksforGeeks"
string(3) "GFG"
string(9) "Hello GFG"
NULL
Reference: https://www.php.net/manual/en/splobjectstorage.offsetget.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...