instanceof only works with objects. So if you want to check if a variable is an object that implements a certain interface, you need two checks: is_object and instanceof.
A fatal error occurs if instanceof is used with other data types.
类型运算符PHP 有一个类型运算符:instanceof。instanceof 用来测定一个给定的对象是否来自指定的对象类。 instanceof 运算符是 PHP 5 引进的。在此之前用 is_a(),但是 is_a() 已经过时了,最好用 instanceof。
由于 $thing 是类型 A 的一个 object,而不是 B 的,只有和类型 A 相符合的程序块被运行:
参见 get_class() 和 is_a()。
add a note
User Contributed Notes
w dot bogaerts at kratz dot nl
22-Sep-2006 04:32
instanceof only works with objects. So if you want to check if a variable is an object that implements a certain interface, you need two checks: is_object and instanceof.
MikeH
10-Mar-2006 07:14
This will fail:
arnold at bean-it dot nl
25-Feb-2006 07:28
Note that operator 'instanceof' will trigger the autoloader.
Colin
06-Dec-2005 03:20
To MooGoo:
klaazvaag . gmail . com
03-Oct-2005 05:02
If you wish to do a short but dynamic comparison using instanceof, you can use this code:
MooGoo
01-Sep-2005 10:42
instanceof can compare a class instance to a string which contains a class name. However that string must be contained in a variable, it will not work as a return value from a function (for instance, get_class()) or even from a regular inline string.
archanglmr at yahoo dot com
18-Feb-2005 10:37
Negated instanceof doesn't seem to be documented. When I read instanceof I think of it as a compairson operator (which I suppose it's not).
d dot schneider at 24you dot de
19-Dec-2004 04:42
use this for cross-version development...
glen at arkadia-systems dot com
11-Sep-2004 01:00
If you are updating code to replace 'is_a()' with the PHP5 compliant
zimba dot spam at gmail dot com
11-Sep-2004 03:17
instanceof cannot use a string for the comparison like is_a.
"Leo Pedretti" <lpedretti at suserver dot com>
24-Aug-2004 11:59
The instanceof operator also checks the interface tree. For example, the following code:
Jesse Scott (scotje at wwc dot edu)
07-Jul-2004 09:52
Since it's not stated authoritatively here, I'll add that instanceof *does* check all the way up the inheritence tree. | ||||