is_a
Checks whether an object belongs to a class or one of its children.
Syntax
is_a(mixed $object_or_class, string $class, bool $allow_string = false): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $object_or_class | mixed | An object or a class name. |
| $class | string | The class or interface name. |
| $allow_string | bool | Optional. Allow a class name string as the first argument. |
Return value
bool`true` if the object is of the class.
Examples
A class name in a variable:
<?php
$type = 'Countable';
var_dump(is_a(new ArrayObject(), $type));
var_dump(is_a('ArrayObject', 'Countable', true));Output:
bool(true)
bool(true)Notes
When the class name is known in advance, write it more briefly: $object instanceof Countable.
See also
- get_class — Returns the class name of an object.
- is_subclass_of — Checks whether a class is a child of another class.
- class_exists — Checks whether a class is defined.
In the tutorial
- Inheritance — Class inheritance in PHP — extends, protected, overriding methods, parent and polymorphism.