The feature illustrated below operates on PHP values, objects, or language metadata.
What Is is_object in PHP?
Returns true when the supplied value is a PHP object; arrays and scalar values are not objects.
is_object(mixed $value) : bool
Parameters:
$value— Required. The value to store, compare or inspect.
Return type: bool. An object may implement Traversable or ArrayAccess, but it remains an object rather than an array.
Example 1: Understanding the Basic Behavior
<?php
var_dump(is_object(new stdClass()));
Output:
bool(true)
Explanation: Returns true when the supplied value is a PHP object; arrays and scalar values are not objects.
Example 2: Testing Another Case
<?php
var_dump(is_object(['name'=>'Alex']));
Output:
bool(false)
Explanation: An object may implement Traversable or ArrayAccess, but it remains an object rather than an array.
Practical Example: Applying the Operation
<?php
$value = json_decode('{"enabled":true}');
if (is_object($value)) { echo $value->enabled ? 'enabled' : 'disabled'; }
Output:
enabled
Explanation: Do not infer that an object implements a specific interface from its type alone.
Important Behavior and Edge Cases
- An object may implement Traversable or ArrayAccess, but it remains an object rather than an array.
- Do not infer that an object implements a specific interface from its type alone.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
is_object | Returns true when the supplied value is a PHP object; arrays and scalar values are not objects. |
is_array() | is_object() accepts objects, whereas is_array() checks PHP arrays. An object implementing ArrayAccess is still not an array. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using array-only operations on objects without implementing the required interfaces. | An object may implement Traversable or ArrayAccess, but it remains an object rather than an array. |
| Choosing is_array() without checking whether it provides the same result. | is_object() accepts objects, whereas is_array() checks PHP arrays. An object implementing ArrayAccess is still not an array. |
Key Takeaways
- Returns true when the supplied value is a PHP object; arrays and scalar values are not objects.
- An object may implement Traversable or ArrayAccess, but it remains an object rather than an array.
- Do not infer that an object implements a specific interface from its type alone.
The examples demonstrate where is_object fits within php programming. Verify the relevant preconditions whenever you adapt the code to real data.