PHP instanceof 关键字
示例
检查一个对象是否属于某个特定类
<?php
class MyClass {}
class AnotherClass extends MyClass{}
$obj = new AnotherClass();
if($obj instanceof AnotherClass) {
echo "该对象是 AnotherClass";
}
// 该对象也是其父类的实例
if($obj instanceof MyClass) {
echo "该对象是 MyClass<br>";
}
?>
自己动手试一试 »
定义和用法
The instanceof
keyword is used to check if an object belongs to a class. The comparison returns true if the object is an instance of the class, it returns false if it is not.
相关页面
了解更多关于对象和类请阅读我们的 PHP OOP 教程。
❮ PHP 关键字