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 programs illustrate the SplObjectStorage::count() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare Empty SplObjectStorage
$gfg = new SplObjectStorage();
$gfg1 = new StdClass;
$gfg2 = new StdClass;
$gfg3 = new StdClass;
  
// Add object to SplObjectStorage class
$gfg->attach($gfg1);
$gfg->attach($gfg2);
$gfg->attach($gfg3);
  
// Use count() function to count object
var_dump($gfg->count());
?>

chevron_right


Output:

int(3)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare Empty SplObjectStorage
$gfg = new SplObjectStorage();
$gfg1 = new StdClass;
$gfg2 = new StdClass;
$gfg3 = new StdClass;
  
// Add object to SplObjectStorage class
$gfg->attach($gfg1);
$gfg->attach($gfg2);
$gfg->attach($gfg3);
  
// Use count in different way
// Passing object as parameter
var_dump(count($gfg));
?>

chevron_right


Output:

int(3)

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.