Detailed explanation of the instanceof keyword and the function of instanceof keyword in PHP, instanceof keyword_PHP tutorial

WBOY
Release: 2016-07-12 09:05:29
Original
833 people have browsed it

Detailed explanation of the instanceof keyword and the role of the instanceof keyword in PHP. The instanceof keyword

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";
Copy after login

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
Copy after login

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:

<&#63;php
$obj = new A();
if ($obj instanceof A) {
  echo 'A';
}
Copy after login

Second usage:

<&#63;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';
} 
&#63;>
Copy after login

Output result: Yes, it is

In addition, please note the difference between instanceof and is_subclass_of(), please see the code:

<&#63;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
&#63;>
Copy after login

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1068825.htmlTechArticleExplain in detail the instanceof keyword and the role of the instanceof keyword in PHP. Another new member of the instanceof keyword PHP5 is instdnceof keyword. Use this keyword to identify a pair...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!