is_a() is used to check whether an object is an instance of a specific class or inherits from that class. The function signature is as follows:
is_a(object $object, string $class_name, bool $allow_string = false): bool
$object: The object to be checked (or class name, depending on $allow_string).
$class_name: The target class name as a string.
$allow_string: Whether the first parameter is allowed to be a class name string instead of an object. The default value is false.
For example:
class Animal {}
class Dog extends Animal {}
<p>$dog = new Dog();<br>
var_dump(is_a($dog, 'Animal')); // true<br>
var_dump(is_a($dog, 'Dog')); // true<br>
PHP class names are case-insensitive, but the class name string passed to is_a() might cause confusion due to case sensitivity, especially when the class name is passed dynamically.
Example:
$className = 'dog'; // lowercase
var_dump(is_a($dog, $className)); // false
Although Dog and dog represent the same class, the is_a() function returns false due to its implementation mechanism.
Solution:
Ensure that the dynamically passed class name uses the correct case, or standardize it to the real class name. You can use get_class() or class_exists() to validate first:
$className = 'dog';
if (class_exists(ucfirst($className))) {
$className = ucfirst($className);
}
var_dump(is_a($dog, $className)); // true
When the first parameter is a class name string, is_a() will return false by default, instead of performing the check.
var_dump(is_a('Dog', 'Animal')); // false
To perform the check based on the class name string, you need to pass the third parameter as true:
var_dump(is_a('Dog', 'Animal', true)); // true
This is particularly important in scenarios that rely on dynamically checking class relationships using strings.
Dynamic class names often include namespaces. In this case, you must use the full namespace class name.
namespace m66\Animals;
<p>class Dog {}</p>
<p>$dog = new Dog();<br>
var_dump(is_a($dog, 'm66\Animals\Dog')); // true<br>
If the dynamically passed class name does not include the namespace, the check will fail.
If the dynamically passed class name has not been loaded yet, is_a() will also return false. Make sure the relevant class is included or loaded via an autoloader.
<?php
<p>namespace m66\net;</p>
<p>class Animal {}<br>
class Dog extends Animal {}</p>
<p>function checkInstance($obj, $dynamicClassName)<br>
{<br>
// Standardize dynamic class name<br>
$dynamicClassName = trim($dynamicClassName);<br>
if (!class_exists($dynamicClassName)) {<br>
// Assuming autoload mechanism, simplified here to throw error<br>
throw new \Exception("Class $dynamicClassName does not exist");<br>
}<br>
return is_a($obj, $dynamicClassName);<br>
}</p>
<p>$dog = new Dog();</p>
<p data-is-last-node="" data-is-only-node="">try {<br>
$result = checkInstance($dog, 'm66\net\Animal');<br>
echo $result ? 'Is Animal or a subclass' : 'Is not Animal class';<br>
} catch (\Exception $e) {<br>
echo $e->getMessage();<br>
}<br>