Monday, 26 August 2013

PHP Function is_subclass_of()

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.

Paramters

ParameterDescription
objectRequired. The tested object
classRequired. 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<br>";
} else {
  echo "no, \$WFC is not a subclass of WidgetFactory<br>";
}


if (is_subclass_of($WF, 'WidgetFactory')) {
  echo "yes, \$WF is a subclass of WidgetFactory<br>";
} else {
  echo "no, \$WF is not a subclass of WidgetFactory<br>";
}
?> 
It will produce following result:
yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory

No comments:

Post a Comment