In this article, you will learn how to check the class of an object in PHP. ThThe instanceof keyword in PHP is used to check if an object belongs to a class. The comparison returns true if the object is an instance of the class, it returns false if it is not.
examples of the INSTANCEOF keyword
Example 1. In this example, we check whether an object belongs to a specific class.
<?php
class MyClass {}
class AnotherClass extends MyClass{}
$obj = new AnotherClass();
if($obj instanceof AnotherClass) {
echo "The object is AnotherClass";
}
// The object is also an instance of the class it is derived from
if($obj instanceof MyClass) {
echo "The object is MyClass<br>";
}
?>