PHP Function Reference

PHP is_a() Function



The PHP is_a() function is used to check if the given object_or_class is of this class or has this class as one of its parents.

Syntax

is_a(object_or_class, class, allow_string)

Parameters

object_or_class Required. Specify a class name or an object instance.
class Required. Specify the class name.
allow_string Optional. If set to false, string class name as object_or_class is not allowed. This also prevents from calling autoloader if the class doesn't exist.

Return Value

Returns true if the object is of this class or has this class as one of its parents, false otherwise.

Example: is_a() example

The example below shows the usage of is_a() function.

<?php
//defining a class
class WidgetFactory {
  //codes
}

//creating a new object
$WF = new WidgetFactory();

if (is_a($WF, 'WidgetFactory')) {
  echo "Yes, \$WF is still a WidgetFactory.";
}
?>

The output of the above code will be:

Yes, $WF is still a WidgetFactory.

Example: using the instanceof operator

Consider the example below where the instanceof operator is used in the similar way.

<?php
//defining a class
class WidgetFactory {
  //codes
}

//creating a new object
$WF = new WidgetFactory();

if ($WF instanceof WidgetFactory) {
  echo 'Yes, $WF is a WidgetFactory.';
}
?>

The output of the above code will be:

Yes, $WF is a WidgetFactory.

❮ PHP Classes/Objects Reference