Type Operators instanceof is used to determine whether a PHP variable is an instantiated object of a certain class/a class that implements an interface
w
0-instanceof is used to determine whether a PHP variable is an instantiated object of a certain class
1-instanceof can also be used to determine whether a variable is an instantiated object of a class that inherits from a parent class
2-Lastly, instanceof can also be used to determine whether a variable is an instantiated object of a class that implements an interface
http://php.net/manual/en/language.operators.type.php
http://php.net/manual/zh/language.operators.type.php
类型运算符
instanceof 用于确定一个 PHP 变量是否属于某一类 class 的实例
instanceof 也可用来确定一个变量是不是继承自某一父类的子类的实例
最后,instanceof也可用于确定一个变量是不是实现了某个接口的对象的实例
<?php interface MyInterface { } class MyClass implements MyInterface { } $a = new MyClass; var_dump($a instanceof MyClass); var_dump($a instanceof MyInterface);
bool(true) bool(true)
<?php interface MyInterface { } class MyClass implements MyInterface { } $a = new MyClass; $b = new MyClass; $c = 'MyClass'; $d = 'NotMyClass'; var_dump($a instanceof $b); // $b is an object of class MyClass var_dump($a instanceof $c); // $c is a string 'MyClass' var_dump($a instanceof $d); // $d is a string 'NotMyClass'
bool(true) bool(true) bool(false)
instanceof does not throw any error if the variable being tested is not an object, it simply returns FALSE. Constants, however, are not allowed.
如果被检测的变量不是对象,instanceof 并不发出任何错误信息而是返回 FALSE。不允许用来检测常量。
<?php $a = 1; $b = NULL; $c = imagecreate(5, 5); var_dump($a instanceof stdClass); // $a is an integer var_dump($b instanceof stdClass); // $b is NULL var_dump($c instanceof stdClass); // $c is a resource var_dump(FALSE instanceof stdClass);
bool(false) bool(false) bool(false)

浙公网安备 33010602011771号