What is the instanceof keyword in PHP? How does it work?

Source: Internet
Author: User
Tags case statement
What is the instanceof keyword?

The Instdnceof keyword was added to the PHP5. Use this keyword to determine whether an object is an instance of a class, a subclass of a class, or a specific interface is implemented and manipulated accordingly. In some cases, we want to determine whether a class is a specific type, or whether a particular interface is implemented. The instanceof operator is ideal for this task. The instanceof operator checks three things: whether the instance is a specific type, whether the instance inherits from a particular type, whether the instance or any of his ancestor classes implement a particular interface. For example, suppose you want to know if 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 exits execution. The instanceof keyword is especially useful when working with multiple objects at the same time. For example, you might want to call a function repeatedly, but you want to adjust the behavior of the function based on the object type. You can use the case statement 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 is an instance of class test! <br/> '; Get this valueswitch ($a instanceof test) {case  true:    echo ' yes<br/> ';    break;  Case false:    echo ' no<br/> ';//get the value break    ;} $d =new Testchilern (); if ($d instanceof test) echo ' $d is a subclass of class test! <br/> '; Get this value

What is instanceof in PHP?

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

The first usage:

<?php$obj = new A (); if ($obj instanceof a) {  echo ' a ';}

Second usage:

<?phpinterface 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's not ';}?>

Output result: Yes, it is

Also, be aware of the difference between instanceof and is_subclass_of (), see the code:

<?phpclass 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); Truevar_dump ($b instanceof Foo); Trueecho "instanceof bar\n"; Var_dump ($a instanceof Bar); Falsevar_dump ($b instanceof Bar); Trueecho "Subclass of Foo\n"; Var_dump (is_subclass_of ($a, ' Foo ')); Falsevar_dump (is_subclass_of ($b, ' Foo ')); Trueecho "Subclass of Bar\n"; Var_dump (is_subclass_of ($a, ' Bar ')); Falsevar_dump (is_subclass_of ($b, ' Bar ')); False?>

Output results (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 Bo OL (FALSE) bool (true) subclass of Bar bool (false)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.