instanceof With Variables

<?php

$a = (object) ['b' => 1];
$b = new stdClass();

var_dump($a instanceof $b);
// true

?>

It is possible to compare the type of two objects with instanceof: PHP uses the class of the second operand, or the name of the class passed as a string.

Here, note that the cast operator (object) creates stdClass when applied to array.

See Also

PHP Features

Last updated: 14 July 2026