PHP is_object() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

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);
CallWhat 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

ParameterDescription
valueRequired. The value you want to check. It can be of any type.

Return Value

Return ValueDescription
boolReturns 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'));

How It Works

  1. new stdClass() creates an object. It is an instance of PHP's generic class, so the result is true.
  2. An array is not an object. Even a list of values returns false.
  3. A string is not an object. Text stays text, so the last result is false.
  4. 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.

PHP
<?php
$fn = function () {
    return 1;
};
$cast = (object) ['a' => 1];

var_dump(is_object($fn));
var_dump(is_object($cast));
var_dump(is_object(null));
var_dump(is_object(new DateTime('2024-01-01')));

How It Works

  1. A closure is an object. PHP stores anonymous functions as instances of the Closure class.
  2. (object) turns an array into an object. The cast creates a stdClass, so the result is true.
  3. null is not an object. It means no value, so is_object() returns false.
  4. 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;
    }
}

How It Works

  1. The list mixes three kinds of values. It has an object, a class name string and null.
  2. is_object() runs first. The && operator only continues when the value is really an object.
  3. The Greeter object passes both checks. Its hello() method runs and prints Hello!.
  4. 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().

Next: is_real()

is_real() was an old alias of is_float() that checked whether a value is a floating point .

is_real() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful