Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

PHP | SplObjectStorage count() Function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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:




<?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());
?>


Output:

int(3)

Program 2:




<?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));
?>


Output:

int(3)

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


My Personal Notes arrow_drop_up
Last Updated : 23 Aug, 2019
Like Article
Save Article
Similar Reads
Related Tutorials