PHP SplObjectStorage attach() Function Last Updated : 23 Jun, 2023 Comments Improve Suggest changes Like Article Like Report The SplObjectStorage::attach() function is an inbuilt function in PHP which is used to add objects into the SplObjectStorage. Syntax: void SplObjectStorage::attach($obj, $val) Parameters: This function accepts two parameters as mention above and described below. $obj: This is required parameter which specifies the object of the storage class. $val: This is an optional parameter which specified the values to be added. Return Value: This function does not returns any value. Below programs illustrate the SplObjectStorage::attach() function in PHP: Program 1: php <?php // Declare new object $obj = new StdClass; // Create an empty storage class $str = new SplObjectStorage(); // Attach $obj with String "GeeksforGeeks" $str->attach($obj, "GeeksforGeeks"); // Print Result var_dump($str[$obj]); ?> Output: string(13) "GeeksforGeeks" Program 2: php <?php // Creating std 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 attach() function $str[$obj3] = "GeeksforGeeks"; $str[$obj4] = NULL ; // Print Result var_dump($str[$obj1]); var_dump($str[$obj2]); var_dump($str[$obj3]); var_dump($str[$obj4]); ?> Output: NULL string(3) "GFG" string(13) "GeeksforGeeks" NULL Reference: https://www.php.net/manual/en/splobjectstorage.attach.php Comment More infoAdvertise with us Next Article PHP SplObjectStorage attach() Function R R_Raj Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP SplObjectStorage detach() Function 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 f 1 min read PHP SplObjectStorage addAll() Function The SplObjectStorage::addAll() function is an inbuilt function in PHP which is used to add elements from another storage. Syntax: void SplObjectStorage::addAll( $value ) Parameters: This function accepts a single parameter $value which holds an storage which need to import. Return Value: It does not 1 min read PHP SplObjectStorage count() Function The SplObjectStorage::count() function is an inbuilt function in PHP which is used to count the number of objects in storage. Syntax: int SplObjectStorage::count() Parameters: This function does not contains any parameter. Return Value: This function returns number of objects in storage. Below progr 1 min read PHP SplObjectStorage contains() Function The SplObjectStorage::contains() function is an inbuilt function in PHP which is used to check the storage object contains a specified object or not. Syntax: bool SplObjectStorage::contains( $value ) Parameters: This function accepts a single parameter $value which specifies the storage object which 1 min read PHP SplObjectStorage current() Function The SplObjectStorage::current() function is an inbuilt function in PHP which is used get the current entry of storage. Syntax: object SplObjectStorage::current() Parameters: This function does not accept any parameter. Return Value: This function returns the object of the current storage. Below prog 1 min read PHP SplObjectStorage key() Function The SplObjectStorage::key() function is an inbuilt function in PHP which is used to get the index of the currently pointing iterator. Syntax: int SplObjectStorage::key() Parameters: This function does not accept any parameter. Return Value: This function returns the index at which the iterator curre 1 min read PHP SplObjectStorage next() Function The SplObjectStorage::next() function is an inbuilt function in PHP which is used to move to next entry of storage. Syntax: void SplObjectStorage::next() Parameters: This function does not accept any parameter. Return Value: This function does not return any value. Below programs illustrate the SplO 1 min read PHP SplObjectStorage valid() Function The SplObjectStorage::valid() function is an inbuilt function in PHP which is used to check the current storage entry is valid or not. Syntax: bool SplObjectStorage::valid() Parameters: This function does not accept any parameter. Return Value: This function returns true if the iterator entry is val 1 min read PHP SplObjectStorage rewind() Function The SplObjectStorage::rewind() function is an inbuilt function in PHP which is used to rewind the iterator to the first storage element. Syntax: void SplObjectStorage::rewind() Parameters: This function does not accept any parameter. Return Value: This function does not return any value. Below progr 1 min read Like