Instanceof is used to determine whether a PHP variable belongs to an instance of a class class:
Example #1 use instanceof for classes
<?php class MyClass { } class Notmyclass { } $a = new MyClass; Var_dump ($a instanceof MyClass); Var_dump ($a instanceof notmyclass);? >
The above routines will output:
BOOL (TRUE)
BOOL (FALSE)
Instanceof can also be used to determine whether a variable is inherited from an instance of a subclass of a parent class:
Example #2 use instanceof for inheriting classes
<?php class ParentClass { } class MyClass extends ParentClass { } $a = new MyClass; Var_dump ($a instanceof MyClass); Var_dump ($a instanceof parentclass);? >
The above routines will output:
BOOL (TRUE)
BOOL (TRUE)
Check if an object is not an instance of a class, you can use the logical operator not.
Example #3 use instanceof to check that an object is not an instance of a class
<?php class MyClass { } $a = new MyClass; Var_dump (! ( $a instanceof StdClass));? >
The above routines will output:
BOOL (TRUE)
Finally, instanceof can also be used to determine if a variable is an instance of an object that implements an interface:
Example #4 use instanceof for interfaces
<?php interface MyInterface { } class MyClass implements MyInterface { } $a = new MyClass; Var_dump ($a instanceof MyClass); Var_dump ($a instanceof myinterface);? >
The above routines will output:
BOOL (TRUE)
BOOL (TRUE)
Although instanceof is usually used directly with the class name, you can also use an object or string variable:
<?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 instance of $d); $d is a string ' Notmyclass '?>
The above routines will output:
BOOL (TRUE)
BOOL (TRUE)
BOOL (FALSE)
If the variable being detected is not an object, instanceof does not emit any error messages but returns FALSE. It is not allowed to detect constants.
Example #6 using instanceof to detect other variables
<?php $a = 1; $b = NULL; $c = Imagecreate (5, 5); Var_dump ($a instanceof stdClass); $a is an integer var_dump ($b instanceof StdClass);//$b was NULL var_dump ($c instanceof StdClass);//$c is a Resource var_dump (FALSE instanceof stdClass);? >
The above routines will output:
BOOL (FALSE)
BOOL (FALSE)
BOOL (FALSE)
PHP Fatal Error:instanceof expects an object instance, constant given
However, the use of instanceof has some pitfalls to understand. Before PHP 5.1.0, if the class name to be checked does not exist, instanceof calls __autoload (). In addition, a fatal error occurs if the class is not loaded. You can avoid this problem by using a dynamic class reference or a string variable that contains a class name:
Example #7 Avoid class name lookups and fatal error problems caused by instanceof in PHP 5.0
<?php $d = ' notmyclass '; Var_dump ($a instanceof $d); No fatal error here?>
The above routines will output:
BOOL (FALSE)
The instanceof operator was introduced in PHP 5. Before this, is_a () was used, but later is_a () was discarded and replaced with instanceof. Note that since PHP 5.3.0, the use of Is_a () has been resumed.