Friday, November 14, 2025
HomeLanguagesPHP SplObjectStorage offsetUnset() Function

PHP SplObjectStorage offsetUnset() Function

The SplObjectStorage::offsetUnset() function is an inbuilt function in PHP that 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. 

The below programs illustrate the SplObjectStorage::offsetUnset() function in PHP.

Program 1: 

php




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


Output:

int(1)
int(0)

Program 2: 

php




<?php
  
// Create an Empty SplObjectStorage
$str = new SplObjectStorage();
   
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$obj4 = new StdClass;
   
$str->attach($obj1, "neveropen");
$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));
?>


Output:

int(4)
int(0)

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

RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7142 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6840 POSTS0 COMMENTS