The problem is that count is intended to count the indexes in an array, not the properties on an object, (unless it's a custom object that implements the Countable interface). Try casting the object, like below, as an array and seeing if that helps.

$total = count((array)$obj);

Simply casting an object as an array won't always work but being a simple stdClass object it should get the job done here.

Answer from Steven Surowiec on Stack Overflow
🌐
PHP
php.net › manual › en › function.count.php
PHP: count - Manual
<?php function getArrCount ($arr, ... count_elt($v,$count); else ++$count; return $count; } ... For a Non Countable Objects $count = count($data); print "Count: $count\n"; Warning: count(): Parameter must be an array or an object that ...
🌐
w3resource
w3resource.com › php › function-reference › count.php
PHP : count() function - w3resource
August 19, 2022 - Note: For objects, if you have SPL installed, you can hook into count() by implementing interface Countable. The interface has exactly one method, Countable::count(), which returns the return value for the count() function. Version: (PHP 4 and above) Syntax: count(array_name, mode) Parameters: Return value: The number of elements in array_name.
🌐
BCCNsoft
doc.bccnsoft.com › docs › php-docs-7-en › function.count.html
Count all elements in an array, or something in an object
If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned. ... count() may return 0 for a variable that isn't set, but it may also return 0 for a variable that has been initialized with an empty array. Use isset() to test if a variable is set. ... <?php $a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count($a); // $result == 3 $b[0] = 7; $b[5] = 9; $b[10] = 11; $result = count($b); // $result == 3 $result = count(null); // $result == 0 $result = count(false); // $result == 1 ?>
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-count-the-number-of-objects-in-a-class-in-PHP.php
How to Count the Number of Objects in a Class in PHP
Every time we instantiate (create) ... as I know, PHP does not have a built-in function that counts the number of objects in a class, so we simply create a custom one and it's very easy to do....
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-arrayobject-count-function
PHP | ArrayObject count() Function - GeeksforGeeks
September 27, 2019 - <?php // PHP program to illustrate the // ArrayObject::count() function // Declare an array and convert it // into array object $arrObject = new ArrayObject( array('Geeks', 'for', 'Geeks') ); // Use ArrayObject::count() function // to count ...
🌐
Pi My Life Up
pimylifeup.com › home › using the count() function in php
Using the count() Function in PHP - Pi My Life Up
November 30, 2022 - The count() function in PHP allows you to count all elements within an array or an object that implements the Countable interface.
🌐
W3Schools
w3schools.com › php › func_array_count.asp
PHP count() Function
connection_aborted() ... usleep() PHP MySQLi · affected_rows autocommit change_user character_set_name close commit connect connect_errno connect_error data_seek debug dump_debug_info errno error error_list fetch_all fetch_array fetch_assoc fetch_field fetch_field_direct fetch_fields fetch_lengths fetch_object fetch_row field_count field_seek ...
🌐
Reintech
reintech.io › blog › comprehensive-guide-php-count-sizeof-functions
A Comprehensive Guide to PHP's `count()` and `sizeof()` Functions
This approach gives you complete control over what "count" means for your object. You could count database records, active connections, or any other meaningful metric. Understanding count() performance characteristics helps you write efficient code: For standard arrays, count() is O(1)—constant time. PHP maintains an internal element counter that's updated during array modifications.
Find elsewhere
🌐
Dev Lateral
devlateral.com › home › guides › php › how to use the php count function
How to use the PHP Count Function | Dev Lateral
August 30, 2023 - The count function is a pre-built ... to count the number of elements in a given value. The count function counts elements inside arrays and objects that are Countable....
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-count-function
PHP count() Function - GeeksforGeeks
April 9, 2025 - The count() function in PHP is used to count the number of elements in an array or the countable properties of an object.
🌐
PHP
durak.org › sean › pubs › software › php-5.3.4 › function.count.html
Count all elements in an array, or properties in an object - PHP 5.34 Documentation
If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned. ... count() may return 0 for a variable that isn't set, but it may also return 0 for a variable that has been initialized with an empty array. Use isset() to test if a variable is set. ... <?php $a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count($a); // $result == 3 $b[0] = 7; $b[5] = 9; $b[10] = 11; $result = count($b); // $result == 3 $result = count(null); // $result == 0 $result = count(false); // $result == 1 ?>
🌐
PHP
durak.org › sean › pubs › software › php-7.4.3 › function.count.html
Count all elements in an array, or something in an object - PHP 7.4.3 Documentation
When the parameter is neither an array nor an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned. ... <?php $a[0] = 1; $a[1] = 3; $a[2] = 5; var_dump(count($a)); $b[0] = 7; $b[5] = 9; $b[10] = 11; var_dump(count($b)); var_dump(count(null)); var_dump(count(false)); ?>