array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.is-a.php', 1 => 'is_a', ), 'up' => array ( 0 => 'ref.classobj.php', 1 => '类/对象 函数', ), 'prev' => array ( 0 => 'function.interface-exists.php', 1 => 'interface_exists', ), 'next' => array ( 0 => 'function.is-subclass-of.php', 1 => 'is_subclass_of', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/classobj/functions/is-a.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
is_a — 检查对象是否属于一个给定的类型或子类型。
确定指定 object_or_class
是否是 class
对象类型,或者是否将 class
作为其中一个超类(父类)。
object_or_class
类名或者实例对象。
class
类名或接口名。
allow_string
如果本参数设置为 false
,object_or_class
就不允许传入字符串类名。
这也会在类不存在时,阻止调用自动加载器(autoloader)。
示例 #1 is_a() 示例
<?php
// 定义类
class WidgetFactory
{
var $oink = 'moo';
}
// 创建新对象
$WF = new WidgetFactory();
if (is_a($WF, 'WidgetFactory')) {
echo "yes, \$WF is still a WidgetFactory\n";
}
?>
示例 #2 使用 instanceof 运算符
<?php
if ($WF instanceof WidgetFactory) {
echo 'Yes, $WF is a WidgetFactory';
}
?>