is_object

Checks whether a variable holds an object instance of any class.

Signature
is_object(mixed $value): bool
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$valuemixedThe variable being evaluated.

Return value

booltrue if value is an object, false otherwise.

Changelog

  • PHP 7.2.0Returns true for unserialized objects with no class definition available (__PHP_Incomplete_Class). Previously false was returned.

is_object() returns true for any object instance regardless of class — your own classes, built-ins like DateTime and ArrayObject, anonymous classes, stdClass from json_decode(), and closures (a closure is an instance of Closure).

When to reach for it

The common case is branching on decoded data: json_decode() returns stdClass by default and arrays with the associative flag, so a helper that handles both starts with is_object(). It is also the right guard before get_object_vars(), get_class() or property access on a mixed value.

What trips people up

is_object() tells you nothing about which class you have. If you care — and you usually do — use instanceof, which checks the class and its parents and interfaces in one step and lets static analysis narrow the type. Reach for is_object() only when any object will do.

Since PHP 7.2 it also returns true for __PHP_Incomplete_Class, the placeholder unserialize() creates when the class definition was not loaded. That object looks like an object but throws on every property access, so after unserializing untrusted data check get_class($v) !== '__PHP_Incomplete_Class' as well.

Note that is_object(null) is false, so a nullable object still needs its own null check.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Handling json_decode() output that may be stdClass or an array
  • Guarding get_object_vars() or get_class() on a mixed value
  • Writing a debug dumper that formats objects differently from arrays

Frequently asked questions

What is the difference between is_object and instanceof?

is_object() is true for any object of any class, while instanceof checks membership of a specific class, parent or interface. Use instanceof whenever you plan to call methods on the value.

Is a closure an object in PHP?

Yes. Every closure and arrow function is an instance of the Closure class, so is_object() returns true for them. Use is_callable() if you want to accept function-name strings and callable arrays too.

Does is_object return true for stdClass?

Yes. stdClass is an ordinary class, so objects from json_decode() or an (object) cast all pass. Use $v instanceof stdClass if you need to single them out.

More examples

Normalising json_decode output

The same JSON decodes to stdClass or an array depending on the second argument. is_object() lets one helper accept either, and get_object_vars() flattens the object to the same shape.

Example · php
<?php
function fields(mixed $data): array {
    if (is_object($data)) {
        return get_object_vars($data);
    }
    return is_array($data) ? $data : [];
}

print_r(fields(json_decode('{"id":1,"name":"Ada"}')));
print_r(fields(json_decode('{"id":1,"name":"Ada"}', true)));
print_r(fields('not a structure'));

is_object versus instanceof

is_object() only opens the door; instanceof is what lets you safely call format(). Checking the interface rather than the concrete class means DateTime and DateTimeImmutable both qualify.

Example · php
<?php
$values = [new DateTimeImmutable('2026-01-01'), new ArrayObject([1]), 'text'];

foreach ($values as $v) {
    if (!is_object($v)) {
        echo get_debug_type($v), ": not an object\n";
        continue;
    }
    echo get_class($v), $v instanceof DateTimeInterface
        ? " -> " . $v->format('Y-m-d') . "\n"
        : " -> generic object\n";
}

Discussion

  • Be the first to comment on this lesson.