If you are updating code to replace 'is_a()' with the PHP5 compliant
'instanceof' operator, you may find it necessary to change the basic
architecture of your code to make this work.
'instanceof' will throw a fatal error if the object being checked for does not
exist.
'is_a()' will simply return false under this condition.
In the example below, 'is_a()' would effectively detect the need to create an
instance of class 'A', but in this case 'instanceof' will crash your program
with a fatal error.
<?php
class B {}
$thing = new B();
if (is_a($thing, 'A')) {
echo 'Yes, $thing is_a A<br>';
} else {
echo 'No, $thing is not is_a A<br>';
}
if ($thing instanceof A) {
echo 'Yes, $thing is an instaceof A<br>';
} else {
echo 'No, $thing is not an instaceof A<br>';
}
?>
>>> Output:
No, $thing is not is_a A
Fatal error: Class 'A' not found in is_a-instanceof.php on line 19
>>>
A suitable work-around for this situation is to check for the existence of the
class before performing the 'instanceof' comparison:
<?php
if (class_exists('A') && $thing instanceof A) {
echo 'Yes, $thing is an instaceof A<br>';
} else {
echo 'No, $thing is not an instaceof A<br>';
}
?>