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 關鍵字