Home  >  Article  >  Backend Development  >  What is the instanceof keyword in PHP? How to use it?

What is the instanceof keyword in PHP? How to use it?

伊谢尔伦
伊谢尔伦Original
2017-06-21 16:36:401922browse

What is the instanceof keyword?

The instdnceof keyword was newly added to PHP5. 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 instanceofoperator 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 a delimiter will result in a syntax error. Second, if the comparison fails, the script will exit from 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 &#39;$m是类test的实例!<br />&#39;; // get this value
switch ($a instanceof test){
  case true :
    echo &#39;YES<br />&#39;;
    break;
  case false :
    echo &#39;No<br />&#39;; //get this value
    break;
}
$d=new testChilern();
if($d instanceof test)echo &#39;$d是类test的子类!<br />&#39;; // get this value

What is the function of instanceof in php

Function: (1) Determine whether an object is an instance of a certain class, (2) Determine whether an object implements an interface.

First usage:

<?php
$obj = new A();
if ($obj instanceof A) {
  echo &#39;A&#39;;
}

Second usage:

<?php
interface ExampleInterface
{
   public function interfaceMethod();
 }
 class ExampleClass implements ExampleInterface
{
   public function interfaceMethod()
   {
     return &#39;Hello World!&#39;;
   }
 }
$exampleInstance = new ExampleClass();
 if($exampleInstance instanceof ExampleInterface){
   echo &#39;Yes, it is&#39;;
 }else{
   echo &#39;No, it is not&#39;;
} 
?>

Output result: Yes, it is

In addition, please pay attention to the difference between instanceof and is_subclass_of(), please see the code:

<?php
class Foo {
   public $foobar = &#39;Foo&#39;;
   public function test() {
     echo $this->foobar . "\n";
   }
 }
 class Bar extends Foo {
   public $foobar = &#39;Bar&#39;;
 }
$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, &#39;Foo&#39;)); // FALSE
var_dump(is_subclass_of($b, &#39;Foo&#39;)); // TRUE
echo "subclass of Bar\n";
var_dump(is_subclass_of($a, &#39;Bar&#39;)); // FALSE
var_dump(is_subclass_of($b, &#39;Bar&#39;)); // 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 is the detailed content of What is the instanceof keyword in PHP? How to use it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn