is_a

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

is_a检查对象是否属于一个给定的类型或子类型。

说明

is_a(mixed $object_or_class, string $class, bool $allow_string = false): bool

检查给定的 object_or_class 是否属于此对象类型或具有此对象类型作为其父级。

参数

object_or_class

类名或者实例对象。

class

类名或接口名。

allow_string

如果本参数设置为 falseobject_or_class 就不允许传入字符串类名。 这也会在类不存在时,阻止调用自动加载器(autoloader)。

返回值

如果对象属于该类或该类是此对象的父类时返回 true,否则返回 false

范例

示例 #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';
}
?>

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top