PHP | SplObjectStorage offsetUnset() Function

The SplObjectStorage::offsetUnset() function is an inbuilt function in PHP which is used to set the object from the storage.

Syntax:

void SplObjectStorage::offsetUnset( $object )

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



Return Value: This function does not return any value.

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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create an empty SplObjectStorage
$str = new SplObjectStorage;
$obj = new StdClass;
  
// Set offset $obj to $str 
$str->attach($obj, "GeeksforGeeks");
  
// Print Result before
var_dump(count($str));
  
// Unset object from storage
$str->offsetUnset($obj);
  
// Print Result after
var_dump(count($str));
  
?>

chevron_right


Output:

int(1)
int(0)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create an Empty SplObjectStorage
$str = new SplObjectStorage();
   
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$obj4 = new StdClass;
   
$str->attach($obj1, "GeksforGeeks");
$str->attach($obj2, "GFG");
$str->attach($obj3);
$str->attach($obj4, "DSA");
   
// Print Result before
var_dump(count($str));
  
// Unset object from storage
$str->offsetUnset($obj1);
$str->offsetUnset($obj2);
$str->offsetUnset($obj3);
$str->offsetUnset($obj4);
  
  
// Print Result after
var_dump(count($str));
?>

chevron_right


Output:

int(4)
int(0)

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