In PHP object-oriented programming, it is often necessary to determine whether an object belongs to a certain class, or to determine the class-level relationship of the object. is_a() and get_class() are two very commonly used functions, combined with them, you can effectively judge the class hierarchy relationship of an object. This article will introduce the usage of these two functions in detail and use examples to illustrate how to use them together.
The is_a() function is used to check whether an object is an instance of a class, or whether the class of the object inherits from that class. Its syntax is as follows:
bool is_a(object $object, string $class_name, bool $allow_string = false)
$object : The object to be checked.
$class_name : target class name.
$allow_string : If true , the first argument is allowed to be a class name string, not just an object.
is_a() will check the class to which the object belongs and its parent class chain. As long as one of them is met, it will return true .
The get_class() function returns the actual class name of an object. The syntax is as follows:
string get_class(object $object)
It only returns the current class name of the object and does not involve the parent class.
Using is_a() alone has been able to determine whether an object belongs to a certain class or its parent class. But sometimes we need to judge:
Whether the object is a specific class (inheritance is not considered)
Whether the object is a subclass of a certain class or this class
Determine the hierarchical differences in inheritance relationships
At this time, the combination of get_class() and is_a() can flexibly implement a variety of judgment logics.
Suppose there is the following class structure:
class Animal {}
class Dog extends Animal {}
class Poodle extends Dog {}
$poodle = new Poodle();
if (is_a($poodle, 'Dog')) {
echo 'This is aDogorDogSubclass instances of';
}
Output:
This is aDogorDogSubclass instances of
if (get_class($poodle) === 'Dog') {
echo 'This is aDogExamples of class';
} else {
echo 'noDogClass instance';
}
Output:
noDogClass instance
if (is_a($poodle, 'Dog') && get_class($poodle) !== 'Dog') {
echo 'This isDogSubclass instances of,但noDogClass itself';
}
Output:
This isDogSubclass instances of,但noDogClass itself
In some business logic, it may be necessary:
Execute logic for a class itself
Execute different logic for subclasses
The joint judgment of is_a() and get_class() can be used to accurately control it.
is_a() supports the third parameter to allow string class name judgment, which is more flexible.
get_class() only accepts object parameters and cannot directly pass strings.
Class names are case sensitive, and pay attention to the consistency of case.
After PHP 5.3.0, is_a() can accept strings as the first parameter and be used with the third parameter true .
Through the combination of is_a() and get_class() , you can flexibly and accurately judge the class hierarchy relationship in PHP:
is_a() determines whether it belongs to a certain class or its subclass.
get_class() determines the specific class name of the object.
Joint use can distinguish specific categories from inheritance categories to meet diversified business needs.