Object-oriented programming (OOP) is a very important part of PHP. Determining whether an object belongs to a certain class or its parent class is a common requirement in daily development. PHP provides two very practical built-in functions is_a() and get_parent_class() to help us achieve this function. This article will introduce the usage of these two functions in detail and demonstrate through examples how to determine whether an object belongs to a certain parent class or inheritance system.
The is_a() function is used to determine whether an object is an instance of a certain class, or a subclass instance of that class. The function prototype is as follows:
bool is_a(object $object, string $class_name, bool $allow_string = false)
$object : The object instance that needs to be detected.
$class_name : The specified class name (string).
$allow_string : Optional parameter, default to false , whether to allow the first parameter to be passed into the class name string (not the object).
Return true if $object is an instance of $class_name , or an instance of its subclass, otherwise false .
<?php
class Animal {}
class Dog extends Animal {}
$dog = new Dog();
var_dump(is_a($dog, 'Animal')); // bool(true)
var_dump(is_a($dog, 'Dog')); // bool(true)
var_dump(is_a($dog, 'Cat')); // bool(false)
?>
In the above example, $dog is an instance of the Dog class, and Dog is a subclass of Animal , so it returns true when judging whether $dog is an Animal type.
get_parent_class() is used to get the parent class name of a class or object. If the class does not have a parent class, false is returned.
Function prototype:
string|false get_parent_class(object|string $class)
The parameters can be an object instance or a class name string.
By calling get_parent_class() recursively, we can determine whether an object is on a certain inheritance chain.
<?php
class Vehicle {}
class Car extends Vehicle {}
class SportsCar extends Car {}
$sportsCar = new SportsCar();
$parent = get_parent_class($sportsCar); // Car
while ($parent !== false) {
echo $parent . PHP_EOL;
$parent = get_parent_class($parent);
}
// Output:
// Car
// Vehicle
?>
Suppose we want to determine whether an object belongs to a Vehicle class or an instance of its subclass, we can perform recursive checking in combination with get_parent_class() :
<?php
function isInstanceOfParent($object, $parentClass) {
$class = get_class($object);
if ($class === $parentClass) {
return true;
}
while ($parent = get_parent_class($class)) {
if ($parent === $parentClass) {
return true;
}
$class = $parent;
}
return false;
}
class Vehicle {}
class Car extends Vehicle {}
class SportsCar extends Car {}
$sportsCar = new SportsCar();
var_dump(isInstanceOfParent($sportsCar, 'Vehicle')); // bool(true)
var_dump(isInstanceOfParent($sportsCar, 'Car')); // bool(true)
var_dump(isInstanceOfParent($sportsCar, 'Animal')); // bool(false)
?>
is_a() is a concise method to determine whether an object is a certain class or its subclass, and is suitable for direct judgment.
get_parent_class() is suitable for obtaining the parent class information of the class and can be used to recursively judge more complex inheritance chains.
For most scenarios, using is_a() is simple and efficient enough.
Combining get_parent_class() can achieve more flexible inheritance relationship judgment logic.
Mastering these two functions can help you better control and check the type and inheritance structure of objects, and improve the robustness and maintainability of your code.