Syntax:void SplObjectStorage::attach ($obj, $val)
Parameters:This function takes two parameters, mentioned above and described below.
- $obj:This is a required parameter that specifies the object of the storage class.
- $val:this is an optional parameter that specifies the values to be added.
Return value:This function does not return no value.The programs below illustrate the
function SplObjectStorage::attach()in PHP:
Program 1:
// Declare a new object
$obj
=
new
StdClass;
// Create empty storage class
$str
=
new
SplObjectStorage();
// Append $obj with the string "GeeksforGeeks"
$str
-> attach (
$obj
,
"GeeksforGeeks"
);
// Print the result
var_dump (
$str
[
$obj
]);
?>
Exit:string (13 ) "GeeksforGeeks"
Program 2:
// Create standard classes
$obj1
=
new
StdClass;
$obj2
=
new
StdClass;
$obj3
=
new
StdClass;
$obj4
=
new
StdClass;
$str
=
new
SplObjectStorage();
$str
-> attach (
$obj1
);
$str
-> attach (
$obj2
,
"GFG"
);
// Another way to use the attach() function
$str
[
$obj3
] =
"GeeksforGeeks"
;
$str
[
$obj4
] = NULL;
// Print the result
var_dump (
$str
[
$obj1
]);
var_dump (
$str
[ $obj2
]);
var_dump (
$str
[ $obj3
]);
var_dump (
$str
[ $obj4
]);
?>
Exit:NULL string ( 3) "GFG" string (13) "GeeksforGeeks" NULL
Link: https://www.php.net/manual/en/splobjectstorage.attach.php