W3docs

instanceof

Learn how the PHP instanceof operator checks whether an object belongs to a class, a parent class, or an implemented interface, with examples and gotchas.

The PHP instanceof Operator

instanceof is a PHP type-checking operator. It returns a boolean (true or false) that tells you whether an object belongs to a given class — or, more usefully, whether it belongs to a parent class up the inheritance chain or implements a given interface.

This page covers the syntax, how instanceof behaves across inheritance and interfaces, how to use it with a class name stored in a variable, the edge cases that surprise people (non-objects, null, unknown class names), and when you should reach for it versus a cleaner alternative.

If classes and objects are new to you, read PHP Classes and Objects first.

Syntax

$object instanceof ClassName

The expression evaluates to true when $object is an instance of ClassName, of any class that extends ClassName, or of any class that implements ClassName (when ClassName is an interface). Otherwise it evaluates to false. Note there is no method-call syntax — instanceof is an operator, like ===, not a function.

A basic check

<?php

class MyClass {}
class MyOtherClass {}

$object = new MyClass();

if ($object instanceof MyClass) {
    echo "The object is an instance of MyClass.";
} else {
    echo "The object is not an instance of MyClass.";
}
// Output: The object is an instance of MyClass.

var_dump($object instanceof MyOtherClass);
// Output: bool(false)

$object was created from MyClass, so the first check is true. It has nothing to do with MyOtherClass, so that check is false. The two classes are unrelated, even though they look similar.

instanceof and inheritance

The real value of instanceof is that it walks up the inheritance chain. A child object is an instance of its parent class, so the check succeeds for every ancestor.

<?php

class Fruit {}
class Apple extends Fruit {}
class Banana extends Fruit {}

$apple = new Apple();

var_dump($apple instanceof Apple);   // bool(true)  — its own class
var_dump($apple instanceof Fruit);   // bool(true)  — its parent class
var_dump($apple instanceof Banana);  // bool(false) — a sibling class

$apple passes the Fruit check because Apple extends Fruit. It fails the Banana check because Apple and Banana are siblings — neither inherits from the other. Learn more in PHP Inheritance.

instanceof and interfaces

instanceof is most commonly used with interfaces. Because any class that implements an interface counts as an instance of it, you can test for a capability without caring about the concrete class.

<?php

interface Drawable {
    public function draw(): string;
}

class Circle implements Drawable {
    public function draw(): string { return "○"; }
}

class Square implements Drawable {
    public function draw(): string { return "□"; }
}

$shapes = [new Circle(), new Square(), "not a shape"];

foreach ($shapes as $shape) {
    if ($shape instanceof Drawable) {
        echo $shape->draw();
    }
}
// Output: ○□

The string "not a shape" is skipped because it is not a Drawable. This is the everyday use case: filter a mixed list down to the objects that support the behavior you need. See PHP Interfaces for the full picture.

Using a class name from a variable

The right-hand side can be a string variable holding a class or interface name. This is handy when the type is determined at runtime.

<?php

class Fruit {}
class Apple extends Fruit {}

$apple = new Apple();
$type  = 'Fruit';

var_dump($apple instanceof $type);   // bool(true)

You can also compare two objects — $a instanceof $b works when $b is an object, checking whether $a is an instance of $b's class.

Edge cases and gotchas

instanceof never throws and never emits a warning. When the left operand is not an object, it simply returns false.

<?php

class Fruit {}

var_dump(null instanceof Fruit);      // bool(false)
var_dump("Fruit" instanceof Fruit);   // bool(false) — a string is not an object
var_dump(42 instanceof Fruit);        // bool(false)

This makes instanceof safe to use as a guard before calling a method, with no need to first check is_object(). If you only need to know whether a value is an object at all (any class), use is_object() instead.

One subtle point: if you pass a class name that does not exist as a string literal on the right-hand side, PHP does not autoload or error — it just returns false. So a typo in the class name fails silently. Prefer the bare class name ($x instanceof Fruit) over a string when you can, so the parser validates it.

When to use instanceof — and when not to

Use instanceof when you genuinely have a value whose type you cannot guarantee: data from json_decode, a mixed collection, a plugin returned by user code, or a catch block narrowing an exception.

<?php

try {
    throw new InvalidArgumentException("bad input");
} catch (Exception $e) {
    if ($e instanceof InvalidArgumentException) {
        echo "Caught an argument error: " . $e->getMessage();
    }
}
// Output: Caught an argument error: bad input

Avoid scattering instanceof checks through your code to branch on type — long if ($x instanceof A) … elseif ($x instanceof B) chains usually mean a method should be defined on each class and called polymorphically instead. Reach for instanceof to guard an operation, not to replace inheritance.

Summary

  • $object instanceof ClassName returns true if the object is an instance of the class, a subclass, or an implemented interface.
  • It walks the full inheritance chain and matches interfaces — that is what makes it powerful.
  • It returns false (never an error) for null, scalars, and other non-objects, so it is safe as a guard.
  • A non-existent class name passed as a string returns false silently; prefer the bare class name.
  • Use it to verify an unknown value's type before acting on it, not as a substitute for polymorphism.

Practice

Practice
What is the usage of the 'instanceof' operator in PHP?
What is the usage of the 'instanceof' operator in PHP?
Was this page helpful?