How to check if stdClass object is empty or not in php?

How to check if stdClass object is empty or not in php?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why empty() Fails on stdClass Objects and How to Fix It

In PHP, checking whether a collection is empty often feels like a straightforward task. However, when dealing with stdClass objects—particularly nested ones or those populated via JSON decoding—the standard empty() function frequently behaves unexpectedly. If you are looking at an array containing a key that maps to an empty stdClass, and empty($object) returns false while sizeof() indicates a count of one, you are encountering a specific quirk in how PHP evaluates object properties versus array keys.

The root cause lies in the distinction between "properties" and "keys." When you instantiate an empty stdClass or decode an empty JSON object {}, the object exists, but it possesses no defined properties. The empty() function, however, relies on a set of specific checks that do not always align with how arrays are evaluated. For standard arrays, empty() returns true if the count is zero. For objects, PHP's implementation of empty() historically returned false if the object itself was instantiated, even if it held no data properties. This discrepancy causes the confusion where sizeof($object) might return 1 (indicating the key exists in the parent array) but the inner object appears empty to your logic.

To accurately determine if an stdClass object contains any data, you must inspect its property count directly. The most robust method is using the built-in count() function on the object itself. Unlike empty(), which triggers a series of internal heuristics that treat an instantiated object as "truthy," count($obj) specifically queries the number of properties assigned to that instance. If the result is zero, the object is genuinely empty; if it returns greater than zero, it contains data.

$cart_items = new stdClass();

// This will return false because the object exists
if (empty($cart_items)) {
    echo "Object exists but might be empty."; 
}

// This correctly identifies the lack of properties
$count = count($cart_items);
if ($count === 0) {
    echo "The stdClass object is empty.";
}

When dealing with nested structures like ['cart_items' => stdClass Object()], you must apply this logic to the specific instance. Accessing the property first ensures you are checking the correct reference. Attempting to use reflection or checking for specific properties (e.g., !isset($obj->id)) is less reliable because an empty object technically lacks all properties, so isset() will always return false regardless of your intended logic. The count() method remains the semantic equivalent of checking an array's length, providing clarity in your conditional statements.

There is a trade-off to consider regarding performance versus readability. While empty() is slightly faster due to internal optimizations for common cases, its behavior on objects is inconsistent across different PHP versions and contexts. Using count() adds negligible overhead in modern environments and guarantees correctness without relying on the ambiguous truthiness of an object instance. If you are working within a Laravel application, this pattern remains consistent regardless of framework version, though you might find similar utility methods in packages like https://laravelcompany.com that abstract these checks for convenience.

Ultimately, relying on empty() for stdClass objects is an anti-pattern that introduces subtle bugs. By switching to count(), you align your code's logic with the actual state of the data structure: does the object hold properties? If not, treat it as empty. This approach prevents logical errors where a populated key in an array masks an empty object within it, ensuring your application handles data structures predictably.

Our network