Detailed PHP instanceof keyword and instanceof keyword what role _php instance

Source: Internet
Author: User

Another new member of the 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 operates 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 a great fit to complete this task. The instanceof operator examines three things: whether an instance is a specific type, whether an instance inherits from a particular type, or whether an instance or any of his ancestor classes implement a specific 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 cause syntax errors. 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 case statements and instanceof keywords to achieve this goal.

Class test{} class
test{}
class Testchilern Extends test{}
$a = new test ();
$m = new test ();
$i = ($m instanceof test);
The IF ($i)
  Echo ' $m is an instance of class test! <br/> '; Get-value
switch ($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 the role of instanceof in PHP

Role: (1) to determine whether an object is an instance of a class, (2) to determine whether an object implements an 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, note the difference between instanceof and is_subclass_of (), see Code:

<?php
class Foo {public
   $foobar = ' foo ';
   The 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 results (PHP 5.4.4):

Use the 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 this article to everybody introduction php instanceof keyword and instanceof keyword has what function the entire content, hoped everybody likes.

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.