PHP is_object() Function

Quick summary: The PHP is_object() function checks whether a variable is an object.

PHP is_object() Syntax

is_object(mixed $value): bool
PHP

PHP is_object() Basic examples

var_dump(is_object(new stdClass()));
PHP
Output:
bool(true)

Returns true for objects.

var_dump(is_object([]));
PHP
Output:
bool(false)

Arrays are not objects.

PHP is_object() Real-world usage

$value = new DateTime();
if (is_object($value)) {
    echo 'Object detected';
}
PHP
Output:
Object detected

Validates object inputs.

PHP is_object() Edge cases

var_dump(is_object(null));
PHP
Output:
bool(false)

Null is not an object.

PHP is_object() Common mistakes

Confusing objects with arrays

Objects and arrays are different types.

Incorrect
is_object(['a'=>1]);
Correct
is_array(['a'=>1]);

Use is_array for arrays.

PHP is_object() Frequently Asked Questions

What does is_object() do?

Checks if a variable is an object.

Return type?

Boolean.

Does it modify variable?

No.

Handles null?

No.

Handles arrays?

No.

Use case?

Type validation.

Performance?

Fast.

Strict?

Yes.

Difference from instanceof?

instanceof checks class.

Handles stdClass?

Yes.

Handles resources?

No.

Alternative?

instanceof.

PHP is_object() Related PHP Functions