Current Location: Home> Latest Articles> Use is_a() to support polymorphic judgment

Use is_a() to support polymorphic judgment

M66 2025-06-02

In PHP programming, polymorphism is an important feature of object-oriented programming. Polymorphism allows objects of different classes to be called with a unified interface, thereby improving code flexibility and maintainability. In order to implement polymorphism, we often need to determine whether an object belongs to a certain class, or whether an interface is implemented. PHP provides a variety of ways to complete this judgment, among which the is_a() function is one of the most commonly used and direct methods.


What is the is_a() function?

The is_a() function is used to check whether an object belongs to a certain class, or whether the object's class inherits from that class, or even implements an interface. The function prototype is as follows:

 bool is_a(object $object, string $class_name, bool $allow_string = false)
  • $object : The object instance to be detected.

  • $class_name : The class name or interface name to be judged.

  • $allow_string (PHP 5.3.0+): Whether the first parameter is allowed to be the string class name (usually not used).

is_a() returns true when an object belongs to a specified class or its subclass, or when a specified interface is implemented, otherwise it returns false .


Example of polymorphic judgment using is_a()

Suppose we have an interface Drawable that defines a drawing behavior. Then there are two classes Circle and Square , both of which implement the Drawable interface.

 <?php
interface Drawable {
    public function draw();
}

class Circle implements Drawable {
    public function draw() {
        echo "Drawing a circle.\n";
    }
}

class Square implements Drawable {
    public function draw() {
        echo "Drawing a square.\n";
    }
}

function renderShape($shape) {
    if (is_a($shape, 'Drawable')) {
        $shape->draw();
    } else {
        echo "The object is not drawable.\n";
    }
}

$circle = new Circle();
$square = new Square();
$stdObj = new stdClass();

renderShape($circle);  // Output: Drawing a circle.
renderShape($square);  // Output: Drawing a square.
renderShape($stdObj);  // Output: The object is not drawable.
?>

Here, we use is_a($shape, 'Drawable') to determine whether $shape implements the Drawable interface, thereby determining whether to call the draw() method.


Comparison of is_a() with instanceof

is_a() and instanceof operator functions are similar, and both can judge the relationship between objects and classes or interfaces. But there are a few differences:

  • is_a() is a function that supports dynamically passing in class name strings, suitable for scenarios where you need to judge based on variable class names.

  • instanceof is a language structure, with a cleaner syntax and slightly better performance, but the class name cannot be dynamically passed (PHP 5.5+ supports variable class name).

  • is_a() can accept class names in string form (when the third parameter allows), instanceof cannot.

Example comparison:

 if (is_a($obj, 'MyClass')) { ... }

if ($obj instanceof MyClass) { ... }

Things to note

  • is_a() can only be used for object judgment and cannot be used to judge ordinary variable types.

  • For interface implementation judgments, is_a() also applies.

  • PHP 7.2+ is recommended to use is_a() , especially for dynamic class names to judge more conveniently.


Conclusion

Using the is_a() function is a concise method to determine whether an object belongs to a certain class or implements a certain interface. It plays an important role in polymorphic programming. Rational use of is_a() can make the code more flexible and extensible and improve the quality of object-oriented programming.


Code sample demonstration address schematic:

 $url = "https://m66.net/example-path";
echo "Sample code reference address:" . $url;