PHP SplObjectStorage key() Function Last Updated : 23 Jun, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 currently pointing. Below programs illustrate the SplObjectStorage::key() function in PHP: Program 1: php <?php // Create an empty SplObjectStorage $str = new SplObjectStorage(); $obj = new StdClass; $str->attach($obj, "d1"); $str->rewind(); // Get current index $index = $str->key(); // Print Result var_dump($index); ?> Output: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, "GeeksforGeeks"); $str->attach($obj2, "GFG"); $str->attach($obj3); $str->attach($obj4, "DSA"); $str->rewind(); // Iterate and print data on each index while($str->valid()) { // Get index $index = $str->key(); $object = $str->current(); $data = $str->getInfo(); var_dump($index, $data); $str->next(); } ?> Output:int(0) string(12) "GeksforGeeks" int(1) string(3) "GFG" int(2) NULL int(3) string(3) "DSA" Reference: https://www.php.net/manual/en/splobjectstorage.key.php Comment More infoAdvertise with us Next Article PHP SplObjectStorage count() Function R R_Raj Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads 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 getinfo() Function The SplObjectStorage::getinfo() function is an inbuilt function in PHP that is used to get the data associated with the object by the current iterator position. Syntax: mixed SplObjectStorage::getinfo() Parameters: This function does not accept any parameter. Return Value: This function returns the 1 min read PHP SplObjectStorage offsetGet() Function The SplObjectStorage::offsetGet() function is an inbuilt function in PHP that is used to get the data associated with the object. Syntax: object SplObjectStorage::offsetGet($obj) Parameters: This function accepts a single parameter $obj which specifies the object to be fetched. Return Value: This fu 1 min read PHP SplObjectStorage offsetSet() Function The SplObjectStorage::offsetSet() function is an inbuilt function in PHP which is used to set the object of storage. Syntax: void SplObjectStorage::offsetSet($obj, $val) Parameters: This function accept two parameters as mention above and described below: $obj: It specifies the object to be attach.$ 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 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 Like