is_subclass_of () function in PHP


The is_subclass_of() function checks if the object has this class as one of its parents.

Syntax

is_subclass_of(object, class, string)

Parameters

  • object − The tested object

  • class − The name of class

  • string − If set to false, the string class name as object is not allowed.

Return

The is_subclass_of() function returns TRUE if the object “object” belongs to a class which is a subclass of “class”, FALSE otherwise.

The following is an example −

Example

 Live Demo

<?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 
"; } else {    echo "no, \$WFC is not a subclass of wid_fact
"; } if (is_subclass_of($WF, 'wid_fact')) {    echo "yes, \$WF is a subclass of wid_fact
"; } else {    echo "no, \$WF is not a subclass of wid_fact
"; } ?>

The following is the output −

Output

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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 24-Jun-2020

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements