PHP - Function is_subclass_of()


Advertisements


Syntax

is_subclass_of ( $object, $class_name );

Definition and Usage

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

Parameters

Sr.No Parameter & Description
1

object(Required)

The tested object

3

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 wid_fact {
      var $oink = 'moo';
   }
   
   // define a child class
   class wid_fact_child extends wid_fact {
      var $oink = 'oink';
   }
   
   // create a new object
   $WF = new wid_fact();
   $WFC = new wid_fact_child();
   
   if (is_subclass_of($WFC, 'wid_fact')) {
      echo "yes, \$WFC is a subclass of wid_fact \n";
   }else {
      echo "no, \$WFC is not a subclass of wid_fact \n";
   }
   
   if (is_subclass_of($WF, 'wid_fact')) {
      echo "yes, \$WF is a subclass of wid_fact \n";
   }else {
      echo "no, \$WF is not a subclass of wid_fact \n";
   }
?> 

It will produce the following result −

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

php_function_reference.htm

Advertisements