Home > php教程 > php手册 > body text

php 类型运算符instanceof

WBOY
Release: 2016-06-13 10:54:32
Original
1273 people have browsed it

类型运算符instanceof在某些php书籍中貌似没有提到啊,在细说php中也只是提到没有细讲,摘抄下手册中的,以作备注。

instanceof 运算符是 PHP 5 引进的。在此之前用 is_a(),但是 is_a() 已经过时了,最好用 instanceof。

 

1、用来确定一个变量是否属于某个类的实例;

2、用来确定一个变量是否是继承自某一父类的子类的实例;

3、用来确定一个变量是否是实现了某个接口的对象的实例。

 

在 PHP 5.1.0之前,如果要检查的类名称不存在,instanceof 会调用 __autoload()。另外,如果该类没有被装载则会产生一个致命错误。可以通过使用动态类引用(dynamic class reference)或用一个包含类名的字符串变量来避开这种问题: 

 

Example #6 避免 PHP 5.0 中 instanceof 引起的类名查找和致命错误问题

 

$d = 'NotMyClass';

var_dump($a instanceof $d); // no fatal error here

?> 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!