Another new member of PHP5 is the instdnceof keyword. Use this keyword to determine whether an object is an instance of a class, a subclass of a class, or implements a specific interface, and perform corresponding operations. In some cases, we want to determine whether a class is of a specific type, or implements a specific interface. The instanceof operator is very suitable for this task. The instanceof operator checks three things: whether the instance is of a specific type, whether the instance inherits from a specific type, and whether the instance or any of its ancestor classes implements a specific interface. For example, suppose you want to know whether an object named manager is an instance of class Employee:
$manager = new Employee(); … if ($manager instanceof Employee) echo "Yes";
There are two points worth noting. First, the class name does not have any delimiters (quotes). Using delimiters will result in a syntax error. Second, if the comparison fails, the script will exit execution. The instanceof keyword is particularly useful when working with multiple objects at the same time. For example, you might call a function repeatedly but want to adjust the function's behavior based on the object type. You can use case statements and the instanceof keyword to achieve this goal.
class test{} class test{} class testChilern Extends test{} $a = new test(); $m = new test(); $i = ($m instanceof test); if($i) echo '$m是类test的实例!<br />'; // get this value switch ($a instanceof test){ case true : echo 'YES<br />'; break; case false : echo 'No<br />'; //get this value break; } $d=new testChilern(); if($d instanceof test)echo '$d是类test的子类!<br />'; // get this value
What is the role of instanceof in php
Function: (1) Determine whether an object is an instance of a certain class, (2) Determine whether an object implements a certain interface.
First usage:
<?php $obj = new A(); if ($obj instanceof A) { echo 'A'; }
Second usage:
<?php interface ExampleInterface { public function interfaceMethod(); } class ExampleClass implements ExampleInterface { public function interfaceMethod() { return 'Hello World!'; } } $exampleInstance = new ExampleClass(); if($exampleInstance instanceof ExampleInterface){ echo 'Yes, it is'; }else{ echo 'No, it is not'; } ?>
Output result: Yes, it is
In addition, please note the difference between instanceof and is_subclass_of(), please see the code:
<?php class Foo { public $foobar = 'Foo'; public function test() { echo $this->foobar . "\n"; } } class Bar extends Foo { public $foobar = 'Bar'; } $a = new Foo(); $b = new Bar(); echo "use of test() method\n"; $a->test(); $b->test(); echo "instanceof Foo\n"; var_dump($a instanceof Foo); // TRUE var_dump($b instanceof Foo); // TRUE echo "instanceof Bar\n"; var_dump($a instanceof Bar); // FALSE var_dump($b instanceof Bar); // TRUE echo "subclass of Foo\n"; var_dump(is_subclass_of($a, 'Foo')); // FALSE var_dump(is_subclass_of($b, 'Foo')); // TRUE echo "subclass of Bar\n"; var_dump(is_subclass_of($a, 'Bar')); // FALSE var_dump(is_subclass_of($b, 'Bar')); // FALSE ?>
Output result (PHP 5.4.4):
use of test() method
Foo
Bar
instanceof Foo
bool(true)
bool(true)
instanceof Bar
bool(false)
bool(true)
subclass of Foo
bool(false)
bool(true)
subclass of Bar
bool(false)
The above content is all the content of the instanceof keyword and the role of the instanceof keyword in PHP introduced in this article. I hope you like it.