PHP Sizeof Object Returns 1

Jan 11, 2012 Gotcha


One of the more common mistakes I have seen in PHP OOP is the incorrect use of sizeof. Unless your class or object implements the Countable Interface, you must convert your object to an array to use sizeof/count on it. The following is directly from’s Manual:

Returns the number of elements in var. 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.

Sizeof/Count Example

Below, we will take a query result, convert to object and typecast to an array:

// Query the database and return 20 rows 
$result = $mysqli->query("SELECT Name FROM City LIMIT 20"); 

// Assign the DB results to $obj 
$obj = $result->fetch_object(); 

// Typecast array to $obj 
if( sizeof( (array) $obj ) > 10 ){ 
  echo "$obj is greater than 10"; 

comments powered by Disqus