is_subclass_of
(PHP 4, PHP 5)
is_subclass_of — 如果此对象是该类的子类,则返回 TRUE
说明
bool is_subclass_of ( object $object , string $class_name )如果对象 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
";
} else {
echo "no, $WFC is not a subclass of WidgetFactory
";
}
if (is_subclass_of($WF, 'WidgetFactory')) {
echo "yes, $WF is a subclass of WidgetFactory
";
} else {
echo "no, $WF is not a subclass of WidgetFactory
";
}
// usable only since PHP 5.0.3
if (is_subclass_of('WidgetFactory_Child', 'WidgetFactory')) {
echo "yes, WidgetFactory_Child is a subclass of WidgetFactory
";
} else {
echo "no, WidgetFactory_Child is not a subclass of WidgetFactory
";
}
?>
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()。