现在的位置: 首页 > 综合 > 正文

PHP 只有一个类型运算符:instanceof

2013年12月10日 ⁄ 综合 ⁄ 共 227字 ⁄ 字号 评论关闭

PHP 只有一个类型运算符: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';
}
?>  

抱歉!评论已关闭.