What is the role of the instanceof keyword and instanceof keyword in PHP? _ php instance

Source: Internet
Author: User
This article mainly introduces the instanceof keyword details in PHP and the relevant information about the role of the instanceof keyword. For more information, see the instdnceof keyword, another new member of PHP5. This keyword can be used to determine whether an object is a class instance or a class subclass, or whether a specific interface is implemented and the corresponding operations are performed. In some cases, we want to determine whether a class is of a specific type or whether a specific interface is implemented. The instanceof operator is ideal for this task. The instanceof operator checks whether an instance is of a specific type, whether the instance inherits from a specific type, and whether the instance or any of its ancestor classes implement a specific interface. For example, if you want to know whether the object named manager is an instance of the 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 (quotation marks ). Using delimiters causes syntax errors. Second, if the comparison fails, the script will exit. The instanceof keyword is particularly useful when processing multiple objects at the same time. For example, you may need to call a function repeatedly, but you want to adjust the function Behavior Based on the object type. You can use the case statement and the instanceof keyword to achieve this goal.

Class test {} class testChilern Extends test {} $ a = new test (); $ m = new test (); $ I = ($ m instanceof test ); if ($ I) echo '$ m is an instance of the test class!
'; // Get this valueswitch ($ a instanceof test) {case true: echo 'Yes
'; Break; case false: echo' No
'; // Get this value break;} $ d = new testChilern (); if ($ d instanceof test) echo' $ d is a subclass of the test class!
'; // Get this value

What is the role of instanceof in php?

Purpose: (1) determine whether an object is an instance of a class, and (2) determine whether an object has implemented an interface.

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

Output result: Yes, it is

In addition, pay attention to the difference between instanceof and is_subclass_of (). Please refer to 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 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 about the functions of the instanceof keyword and instanceof keyword in PHP.

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.