Heim  >  Artikel  >  php教程  >  php 类型运算符instanceof

php 类型运算符instanceof

WBOY
WBOYOriginal
2016-06-13 10:54:321275Durchsuche

类型运算符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

?> 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn