is_subclass_of

(PHP 4, PHP 5, PHP 7)

is_subclass_of锟斤拷锟斤拷硕锟斤拷锟斤拷歉锟斤拷锟斤拷锟斤拷锟洁,锟津返伙拷 TRUE

说锟斤拷

is_subclass_of ( object $object , string $class_name ) : bool

锟斤拷锟斤拷锟斤拷锟� object 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 class_name 锟斤拷锟斤拷锟洁,锟津返伙拷 TRUE锟斤拷锟斤拷锟津返伙拷 FALSE锟斤拷

Note:

锟斤拷 PHP 5.0.3 锟斤拷也锟斤拷锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷锟斤拷指锟斤拷 object 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

Example #1 is_subclass_of() 锟斤拷锟斤拷

<?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";
}

// usable only since PHP 5.0.3
if (is_subclass_of('WidgetFactory_Child''WidgetFactory')) {
  echo 
"yes, WidgetFactory_Child is a subclass of WidgetFactory\n";
} else {
  echo 
"no, WidgetFactory_Child is not a subclass of WidgetFactory\n";
}
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

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

锟轿硷拷 get_class()锟斤拷 get_parent_class() 锟斤拷 is_a()锟斤拷

锟斤拷锟斤拷

object

A class name or an object instance

class_name

The class name

allow_string

If this parameter set to false, string class name as object is not allowed. This also prevents from calling autoloader if the class doesn't exist.

锟斤拷锟斤拷值

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

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.3.9 Added allow_string parameter
5.0.3 You may also specify the object parameter as a string (the name of the class)

锟斤拷锟斤拷

Example #2 is_subclass_of() example

<?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";
}


// usable only since PHP 5.0.3
if (is_subclass_of('WidgetFactory_Child''WidgetFactory')) {
  echo 
"yes, WidgetFactory_Child is a subclass of WidgetFactory\n";
} else {
  echo 
"no, WidgetFactory_Child is not a subclass of WidgetFactory\n";
}
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

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

注锟斤拷

Note:

锟斤拷锟斤拷锟斤拷嗖伙拷锟斤拷锟街拷啵癸拷么撕锟斤拷锟斤拷锟绞癸拷锟斤拷魏锟斤拷锟阶拷锟斤拷 autoloader锟斤拷

锟轿硷拷

  • get_class() - 锟斤拷锟截讹拷锟斤拷锟斤拷锟斤拷锟�
  • get_parent_class() - 锟斤拷锟截讹拷锟斤拷锟斤拷锟侥革拷锟斤拷锟斤拷
  • is_a() - 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷诟锟斤拷锟斤拷锟斤拷锟斤拷谴硕锟斤拷锟侥革拷锟斤拷锟津返伙拷 TRUE
  • class_parents() - 锟斤拷锟斤拷指锟斤拷锟斤拷母锟斤拷唷�