Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

PHP | SplObjectStorage offsetSet() Function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The SplObjectStorage::offsetSet() function is an inbuilt function in PHP which is used to set the object of storage. Syntax:

void SplObjectStorage::offsetSet($obj, $val)

Parameters: This function accept two parameters as mention above and described below:

  • $obj: It specifies the object to be attach.
  • $val: It specifies the value to be associate with the object.

Return Value: This function does not return any value. Below programs illustrate the SplObjectStorage::offsetSet() function in PHP: Program 1: 

php




<?php
 
// Create an empty SplObjectStorage
$str = new SplObjectStorage;
$obj = new StdClass;
 
// Set offset $obj to $str
$str->offsetSet($obj, "GeeksforGeeks");
 
// Print Result
var_dump($str->offsetGet($obj));
?>

Output:

string(13) "GeeksforGeeks"

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->offsetSet($obj1, "GeeksforGeeks");
$str->offsetSet($obj2, "GFG");
$str->offsetSet($obj3);
$str->offsetSet($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(12) "GeksforGeeks"
string(3) "GFG"
string(9) "Hello GFG"
NULL

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


My Personal Notes arrow_drop_up
Last Updated : 07 Sep, 2022
Like Article
Save Article
Similar Reads
Related Tutorials