In PHP, is_object() checks whether a value is an object. It returns true for instances of any class, including stdClass objects, closures and built-in classes such as DateTime.
Use it before you read a property or call a method, to make sure the value really is an object. Arrays, strings, numbers and null all return false, even if they hold object-like data.
Syntax
Call is_object() with the values you need. Start with this line.
PHP
$ok = is_object($value);
Call
What it does
is_object(new stdClass())
Returns true because the value is an object.
is_object([1, 2])
Returns false because an array is not an object.
is_object(function () {})
Returns true because every closure is an object of the Closure class.
A class name written as a string, like 'Greeter', is not an object, so is_object('Greeter') returns false. Create an instance with new first.
Library declarations
PHP
is_object(mixed $value): bool
Parameters
Parameter
Description
value
Required. The value you want to check. It can be of any type.
Return Value
Return Value
Description
bool
Returns true if the value is an object, and false for every other type.
PHP Examples
Example 1 — Objects, Arrays and Strings
Create a stdClass object and compare it with an array and a string.
PHP
<?php
$obj = new stdClass();
var_dump(is_object($obj));
var_dump(is_object([1, 2]));
var_dump(is_object('text'));
Output
bool(true)
bool(false)
bool(false)
How It Works
new stdClass() creates an object. It is an instance of PHP's generic class, so the result is true.
An array is not an object. Even a list of values returns false.
A string is not an object. Text stays text, so the last result is false.
Each answer is a bool. var_dump() prints bool(true) or bool(false).
Example 2 — Closures, Casts and Built-in Classes
Some values are objects even when you do not write the word class. Test a closure, a cast, null and DateTime.
A closure is an object. PHP stores anonymous functions as instances of the Closure class.
(object) turns an array into an object. The cast creates a stdClass, so the result is true.
null is not an object. It means no value, so is_object() returns false.
DateTime is a built-in class. Any instance of it counts as an object, so the result is true.
Example 3 — Safe Method Calls
Call a method only when the value is an object that really has that method.
PHP
<?php
class Greeter {
public function hello() {
return 'Hello!';
}
}
$items = [new Greeter(), 'Greeter', null];
foreach ($items as $item) {
if (is_object($item) && method_exists($item, 'hello')) {
echo $item->hello(), PHP_EOL;
} else {
echo 'Cannot call hello()', PHP_EOL;
}
}
Output
Hello!
Cannot call hello()
Cannot call hello()
How It Works
The list mixes three kinds of values. It has an object, a class name string and null.
is_object() runs first. The && operator only continues when the value is really an object.
The Greeter object passes both checks. Its hello() method runs and prints Hello!.
The string and null fail the first check. The else branch prints a message instead of crashing.
Frequently Asked Questions
It checks whether a value is an object. It returns true for any class instance, closures included, and false for arrays, strings, numbers, booleans and null.
No. Arrays and objects are different types in PHP. is_object([1, 2]) returns false, but you can cast an array with (object) to create a stdClass object.
Yes. Anonymous functions are instances of the built-in Closure class, so is_object() returns true for them.
is_object() only tells you that the value is some object. instanceof checks for a specific class or interface, such as $obj instanceof DateTime.
No. A string like 'Greeter' is only text, so is_object('Greeter') returns false. You need an instance created with new Greeter().