PHP SplObjectStorage detach() Function
Last Updated :
23 Jun, 2023
The SplObjectStorage::detach() function is an inbuilt function in PHP which is used to remove objects from the storage.
Syntax:
void SplObjectStorage::detach($obj)
Parameters: This function accepts a single parameter $obj which specifies the object to be remove from the storage.
Return Value: This function does not return any value.
Below programs illustrate the SplObjectStorage::detach() function in PHP:
Program 1:
<?php
$obj = new StdClass;
$str = new SplObjectStorage();
$str ->attach( $obj , "GeeksforGeeks" );
var_dump( count ( $str ));
$str ->detach( $obj );
var_dump( count ( $str ));
?>
|
Program 2:
<?php
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$str = new SplObjectStorage();
$str ->attach( $obj1 , "GeeksforGeeks" );
$str ->attach( $obj2 );
$str ->attach( $obj3 , "GFG" );
var_dump( count ( $str ));
$str ->detach( $obj1 );
var_dump( count ( $str ));
$str ->detach( $obj3 );
var_dump( count ( $str ));
?>
|
Output:
int(3)
int(2)
int(1)
Reference: https://www.php.net/manual/en/splobjectstorage.detach.php
Share your thoughts in the comments
Please Login to comment...