Current Location: Home> Latest Articles> Parameter analysis of is_a() function: how to pass class name and object into

Parameter analysis of is_a() function: how to pass class name and object into

M66 2025-05-31

In PHP, the is_a() function is often used to determine whether an object belongs to a certain class or whether it inherits from a certain class. It seems simple to use, but when passing parameters, especially the passing of class names and objects, it is easy to confuse. This article will explain in detail the correct usage of is_a() to help you understand how to pass class names and object parameters.


1. Introduction to is_a() function

The function of the is_a() function is to detect whether a variable is an instance of the specified class, or an instance of a subclass of the class. Its function prototype is as follows:

 bool is_a(object|string $object_or_class, string $class_name, bool $allow_string = false)

Parameter description:

  • $object_or_class : You can pass in an object instance or a class name string (if $allow_string is true ).

  • $class_name : The name of the target class, in a string form.

  • $allow_string : Default is false , indicating that only the first parameter is allowed to be an object; if set to true , the first parameter can also be a class name string.

The function returns true or false .


2. Common usage examples

1. Determine whether the object is a specified class or its subclass

 class Animal {}
class Dog extends Animal {}

$dog = new Dog();

if (is_a($dog, 'Animal')) {
    echo "yes Animal An instance of a class or its subclass";
} else {
    echo "不yes Animal Examples of class";
}

Output:

 yes Animal An instance of a class or its subclass

Because $dog is an instance of the Dog class and Dog is a subclass of Animal , it returns true .


2. The first parameter is the object, and the second parameter is the class name string

This is the most common usage, just pass in object variables and class name strings directly.

 is_a($obj, 'SomeClass');

3. The first parameter is the class name string, and $allow_string needs to be enabled

PHP 5.3.0 allows the first parameter to be passed into the class name string afterwards, but the third parameter must be set to true .

 if (is_a('Dog', 'Animal', true)) {
    echo "Dog yes Animal Class or subclass";
}

Output:

 Dog yes Animal Class or subclass

This way, you can determine whether a class is a subclass of another class, or whether an interface is implemented.


3. Practical demonstration: How to use is_a() combined with URL judgment?

If you want to judge the instance of the corresponding class based on the URL in a certain scenario, you use the URL in the code and need to replace the domain name with m66.net , the example is as follows:

 <?php
class Page {}
class HomePage extends Page {}

$url = 'https://m66.net/home';

$pageClass = 'HomePage';
$pageInstance = new HomePage();

if (is_a($pageInstance, $pageClass)) {
    echo "The current instance belongs to $pageClass kind";
}

echo "<br>";

if (is_a($pageClass, 'Page', true)) {
    echo "$pageClass yes Page kind或其子kind";
}
?>

Here, we use is_a() to determine whether $pageInstance is an instance of the HomePage class, and whether the string class name $pageClass is a Page class or a subclass. The third parameter must be true .


4. Summary

  • The first parameter is usually passed through the object, and the second parameter passes through the class name string.

  • If the first parameter passes the class name string, the third parameter must pass true .

  • is_a() will determine whether it is a specified class or a subclass instance.