W3docs

is_object()

The PHP is_object() function checks whether a variable is an object. Learn its syntax, return value, common gotchas (json_decode, casts, closures), and how it differs from instanceof.

Introduction

is_object() is a built-in PHP function that tests whether a variable holds an object — an instance of a class. It returns a boolean, so it is most often used in an if statement to guard code before you call a method or read a property on a value whose type you are not sure of.

An object is a value created from a class with new (or returned by a factory, a database driver, json_decode(), etc.). Calling a method on something that is not an object raises a fatal Error, so checking first is a common defensive pattern when handling input of unknown shape.

Syntax

is_object(mixed $value): bool
  • $value — the variable to test. Any type is accepted.
  • Returns true if $value is an object, false for every other type (string, int, array, null, etc.).

Basic example

php— editable, runs on the server

Here $obj is an instance of MyClass, so is_object() returns true; $text is a string, so it returns false.

Why var_dump and not echo? When you echo a boolean, true prints as 1 but false prints as an empty string — so echo is_object($text) looks like it printed nothing. var_dump() shows the type explicitly (bool(true) / bool(false)), which makes type checks far easier to read while learning.

Guarding a method call

The most common real use is protecting code that assumes it has an object:

<?php
function describe($value): string {
  if (is_object($value)) {
    return "Got an object of class " . get_class($value);
  }
  return "Not an object: " . gettype($value);
}

echo describe(new MyClass()), "\n"; // Got an object of class MyClass
echo describe("hello"), "\n";       // Not an object: string
echo describe([1, 2, 3]), "\n";     // Not an object: array
?>

Pairing is_object() with get_class() lets you branch on the concrete type only when it is safe to do so.

What counts as an object

Some values that feel like simple data are actually objects in PHP:

<?php
var_dump(is_object((object) ["a" => 1]));   // bool(true)  — array cast to stdClass
var_dump(is_object(json_decode('{"a":1}'))); // bool(true)  — default decode returns an object
var_dump(is_object(fn() => 1));              // bool(true)  — closures are Closure objects
var_dump(is_object(null));                   // bool(false)
var_dump(is_object([1, 2, 3]));              // bool(false) — arrays are NOT objects
?>

The last two are the usual gotchas: null and arrays are not objects. If you need to distinguish those, use is_null() and is_array().

FunctionChecks for
is_object()an object instance
is_array()an array
is_string()a string
is_int()an integer
is_null()the null value
gettype()returns the type name as a string

If you need an instanceof test for a specific class rather than "any object", use the instanceof operator instead — is_object() only tells you that the value is some object.

Conclusion

is_object() answers one narrow question: is this value an object? Use it to guard method and property access against the fatal errors you get from calling methods on strings, arrays, or null. Remember the surprises — casts to (object), json_decode() results, and closures are all objects, while arrays and null are not. For type-specific work, reach for instanceof; for a readable type name, reach for gettype().

Practice

Practice
What is the function of is_object() in PHP?
What is the function of is_object() in PHP?
Was this page helpful?