What is PHP is_a() Function?
If you want to determine whether a class of an object or a class
- is a sub class of a parent class (or itself)
- or, implements an interface,
then, use is_subclass_of() function.
Syntax:
is_a(object_or_class, class_or_interface, allow_string)
Parameters:
The Function has 2 required parameters and 1 optional parameter-
object_or_class (Required): It specifies an object or a class.
class_or_interface (Required): It specifies a parent class (or the class itself) or interface of the 1st parameter.
allow_string(Optional): It specifies a boolean value(TRUE or FALSE) which controls 2 things-
- TRUE – if it is TRUE, the first parameter can be either a class or an object. Also, if the class doesn’t exist, it will trigger the auto loader to load the class.
- FALSE – if it is FALSE (It is default), the first parameter must be an object, meaning it doesn’t allow a string class. Also, the class prevent calling auto loader if the class in the 1st parameter doesn’t exist.
Return Values:
The function returns–
- TRUE, if the object or class name belongs to the specified class or a class that extends it
- FALSE otherwise.
Examples:
Example 1:
<?php
class Human {
var $hand = 2;
}
class Male extends Human {
var $hormones = 'Testosterone';
}
$objMale = new Male();
if (is_a('Male', 'Human')) { echo "The class 'Male' is a subclass of 'Human'.<br />"; }
else { echo "The class 'Male' is not subclass of 'Human'.<br />"; }
if (is_a($objMale, 'Human')) { echo "The class of the object '\$objMale' is subclass of 'Human'.<br />"; }
else { echo "Male is not subclass of Human.<br />"; }
if (is_a($objMale, 'Male')) { echo "The object '\$objMale' is subclass of its own class 'Male'.<br />"; }
else { echo "The object '\$objMale' is subclass of its own class 'Male'"; }
?>
Output:
The class 'Male' is not subclass of 'Human'.
The class of the object '$objMale' is subclass of 'Human'.
The object '$objMale' is subclass of its own class 'Male'.
Example 2:
<?php
interface Human {
public function talk();
}
class Male implements Human {
function talk() {
echo "Hello, John";
}
}
$objMale = new Male;
if (is_a($objMale, 'Human')) { echo "The class of the object '\$objMale' implements interface 'Human'.<br />"; }
else { echo "The class of the object \$objMale doesn't implement Human interface.<br />"; }
if (is_a($objMale, 'Animal')) { echo "The class of the object '\$objMale' implements interface 'Animal'.<br />"; }
else { echo "The class of the object '\$objMale' doesn't implement interface 'Animal'.<br />"; }
if (is_a($objMale, 'Male')) { echo "The class of the object '\$objMale' implements interface 'Human'.<br />"; }
else { echo "The class of the object '\$objMale' doesn't implement interface 'Human'.<br />"; }
if (is_a('Male', 'Human', TRUE)) { echo "class 'Male' implements interface 'Human'.<br />"; }
else { echo "Male does't implement interface 'Human'.<br />"; }
?>
Output:
The class of the object '$objMale' implements interface 'Human'.
The class of the object '$objMale' doesn't implement interface 'Animal'.
The class of the object '$objMale' implements interface 'Human'.
class 'Male' implements interface 'Human'.
Practical Usages of is_a() Function:
The function is a very powerful tool in object oriented programming to verify the interface, inheritance relationship.
PHP Version Support:
PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
Summary: PHP is_a() Function
is_a() is a class/object type function in PHP. Use this function to find out sub class of a parent class or object of class.