instanceof is a Type operator in PHP. It is used to determine whether a PHP variable is an
instantiated object of a certain
class.
Syntax
($variable instanceof Class_Name)
<?php
class Class_A
{
}
class Class_B
{
}
$obj_A = new Class_A;
var_dump($obj_A instanceof Class_A);
echo "<br>";
var_dump($obj_A instanceof Class_B);
?>
Output
bool(true)
bool(false)
Instanceof with inherited classes
Example :
<?php
class Class_A
{
}
class Class_B extends Class_A
{
}
$obj = new Class_B;
var_dump($obj instanceof Class_A);
echo "<br>";
var_dump($obj instanceof Class_B);
?>
Output
bool(true)
bool(true)