Definition:
In a PHP program, the instanceof operator is used either within a function or outside of one.
When a class is defined, the instanceof operator checks whether a given object belongs to that class.
If the object belongs to that class, it returns true.
Otherwise, it returns false.
When you use this operator inside a function, you can store its result in a variable if needed, and check whether the object belongs to a particular class.
Syntax:
object instanceof ClassName It returns: true → if the object belongs to that class (or parent class/interface) false → if the object does NOT belong to that class
Example:
<?php
class Animal { }
class Dog extends Animal { }
$myDog = new Dog();
var_dump($myDog instanceof Dog);
var_dump($myDog instanceof Animal);
var_dump($myDog instanceof stdClass);
?>
Output:
bool(true) bool(true) bool(false)
