The is_a Function
The is_a
function takes an object and a class name, returning true
if the object is an instance of that class or its descendant. The third parameter can specify whether to consider inheritance (default is true
).
Syntax
is_a(object, class_name, allow_string = true);
Example
Let's check if the object belongs to the stdClass
class:
<?php
$obj = new stdClass();
var_dump(is_a($obj, 'stdClass'));
?>
Code execution result:
true
Example
Let's check class inheritance:
<?php
class ParentClass {}
class ChildClass extends ParentClass {}
$child = new ChildClass();
var_dump(is_a($child, 'ParentClass'));
?>
Code execution result:
true
Example
Let's check the work with interfaces:
<?php
interface MyInterface {}
class MyClass implements MyInterface {}
$obj = new MyClass();
var_dump(is_a($obj, 'MyInterface'));
?>
Code execution result:
true
See Also
-
the is_subclass_of function,
which checks inheritance -
the instanceof function,
the instance check operator -
the get_class function,
which returns the object's class -
the class_implements function,
which returns the class interfaces