複製鏈接
請複製以下鏈接發送給好友

類型運算符

鎖定
類型運算符指的是php 有一個類型運算符:instanceof。instanceof用來測定一個給定的對象是否來自指定的對象類 [1] 
中文名
類型運算符
屬    性
計算機術語
instanceof 運算符是 php 5 引進的。在此之前用 is_a(),但是 is_a() 已經過時了,最好用 instanceof。
<?phpclass A { }class B { }$thing = new A;if ($thing instanceof A) { echo 'A';}if ($thing instanceof B) { echo 'B';}?>由於 $thing 是類型 A 的一個 object,而不是 B 的,只有和類型 A 相符合的程序塊被運行:
例子:
class Dog{}
class Cat{}
$cat1 = new Cat;
if($cat1 instanceof Cat){
echo "我是一隻貓!喵喵!";
}
參考資料