Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

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): bool

Parameters

ParameterTypeDescription
$object_or_classmixedAn object or a class name.
$classstringThe class or interface name.
$allow_stringboolOptional. 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

In the tutorial

  • Inheritance — Class inheritance in PHP — extends, protected, overriding methods, parent and polymorphism.

Thanks for reading!

Donate to the site — we work hard for you.