PHP instanceof 关键字
实例
检查一个对象是否属于特定的类:
<?php
class MyClass {}
class AnotherClass extends MyClass{}
$obj = new AnotherClass();
if($obj instanceof AnotherClass) {
echo "The object is AnotherClass";
}
// The object is also an instance of the class it is derived from
if($obj instanceof MyClass) {
echo "The object is MyClass<br>";
}
?>
亲自试一试 »
class MyClass {}
class AnotherClass extends MyClass{}
$obj = new AnotherClass();
if($obj instanceof AnotherClass) {
echo "The object is AnotherClass";
}
// The object is also an instance of the class it is derived from
if($obj instanceof MyClass) {
echo "The object is MyClass<br>";
}
?>
定义和用法
instanceof
关键字用于检查对象是否属于某个类。 如果对象是类的实例,则比较返回 true,否则返回 false。
相关页面
在我们的 PHP OOP 教程中了解有关对象和类的更多信息。