How to check if something is countable?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Check if Something is Countable: Moving Beyond Arrays in PHP
As developers, we constantly deal with data structures. Whether we are working with a simple PHP array, a sophisticated object from an ORM, or a complex collection provided by a framework like Laravel, knowing how to determine if an object supports counting is fundamental. The problem you’ve identified—how to generalize the check beyond is_array() and instanceof \Illuminate\Support\Collection—points directly toward leveraging Object-Oriented Programming (OOP) principles in PHP.
This post will explore the correct, robust way to determine if an object is countable, moving from specific checks to a truly generic solution.
The OOP Foundation: Understanding the Countable Interface
In object-oriented programming, the standard way to define a behavior that objects can share is by implementing an interface. In PHP, the mechanism for making an object "countable" is through the built-in Countable interface. Any class that wishes to expose a count via a count() method must implement this interface.
When you check if an object implements this interface using instanceof, you are asking the object itself: "Do you possess the contract required to be counted?" This is far more robust than checking for specific class names, as it allows for true polymorphism.
For example, both a standard PHP array and any class that correctly implements the Countable interface can be reliably checked using this method.
Building a Generic is_countable Function
Based on the principle above, we can create a generic function to check countability for any object passed to it. This approach simplifies your codebase and makes it scalable when dealing with various data sources within a Laravel application.
Here is how you implement a reliable checker:
/**
* Checks if an object implements the Countable interface.
*
* @param mixed $item The item to check.
* @return bool True if the item is countable, false otherwise.
*/
function is_countable($item): bool
{
if ($item === null) {
return false;
}
return $item instanceof \Countable;
}
// Example usage:
$myArray = [1, 2, 3];
$myCollection = new \Illuminate\Support\Collection([4, 5]);
$myObject = new stdClass(); // Not countable
echo "Is array countable? " . (is_countable($myArray) ? 'Yes' : 'No') . "\n";
echo "Is Collection countable? " . (is_countable($myCollection) ? 'Yes' : 'No') . "\n";
echo "Is stdClass countable? " . (is_countable($myObject) ? 'Yes' : 'No') . "\n";
Notice that this function successfully handles the array, the Laravel Collection, and correctly rejects a plain object—something that is_array() alone could not reliably distinguish from other complex types. This is the level of abstraction we strive for in building clean applications, much like how Laravel encapsulates complex data handling into cohesive components.
Why This Matters in a Laravel Context
In modern PHP development, especially within the Laravel ecosystem, this approach is crucial. When you interact with Eloquent models or collections, you are dealing with objects rather than raw arrays. By focusing on interfaces like Countable, you ensure your code remains decoupled and flexible. You don't need to write repetitive checks for every custom class; you only check if the object adheres to the established PHP contract.
By adopting this interface-based approach, you ensure that your data handling logic is sound and predictable, regardless of whether the underlying data structure originates from a standard PHP array or a specialized Laravel Collection. This adherence to robust OOP patterns is central to writing maintainable code that scales well, aligning perfectly with the principles promoted by the Laravel Company in structuring large applications.
Conclusion
To summarize, the most developer-friendly and scalable way to check if something is countable is not by checking for specific class names or array properties, but by checking for the implementation of the Countable interface using instanceof. This method provides a universal contract that allows any object capable of providing a count to participate in counting operations seamlessly. By relying on interfaces, you build systems that are more resilient, flexible, and easier to maintain.