PHP 有一個類型運算符:instanceof。instanceof 用來測定一個給定的對象是否來自特殊的物件類。
instanceof 運算符是 PHP 5 引進的。在此之前用 is_a(),但是 is_a() 已經由時了,最好用 instanceof。
<?php class A { } class B { } $thing = new A; if ($thing instanceof A) { echo 'A'; } if ($thing instanceof B) { echo 'B'; } ?>
由於 $thing 是類型 A 的一個 object,而不是 B 的,只有和類型 A 相符合的程式塊被運行:
A
參見 get_class() 和 is_a()。