PHP - Function is_subclass_of()


Advertisements


Syntax

is_subclass_of ( $object, $class_name );

Definition and Usage

Checks if the given object has the class class_name as one of its parents.

Parameters

Parameter Description
object Required. The tested object
class Required. The class name.

Return Value

This function returns TRUE if the object object, belongs to a class which is a subclass of class_name, FALSE otherwise.

Example

Following is the usage of this function −

<?php
   // define a class
   class WidgetFactory
   {
      var $oink = 'moo';
   }
   
   // define a child class
   class WidgetFactory_Child extends WidgetFactory
   {
      var $oink = 'oink';
   }
   
   // create a new object
   $WF = new WidgetFactory();
   $WFC = new WidgetFactory_Child();
   
   if (is_subclass_of($WFC, 'WidgetFactory')) {
      echo "yes, \$WFC is a subclass of WidgetFactory \n";
   }
   else
   {
      echo "no, \$WFC is not a subclass of WidgetFactory \n";
   }
   
   if (is_subclass_of($WF, 'WidgetFactory')) {
      echo "yes, \$WF is a subclass of WidgetFactory \n";
   }
   else
   {
      echo "no, \$WF is not a subclass of WidgetFactory \n";
   }
?> 

It will produce the following result −

yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory

php_function_reference.htm