What is the role of the instanceof keyword and instanceof keyword in PHP?

Source: Internet
Author: User
This article mainly introduces the instanceof keyword details in PHP and related information on the role of the instanceof keyword. For more information, see

This article mainly introduces the instanceof keyword details in PHP and related information on the role of the instanceof keyword. For more information, see

Another new member of PHP5 is the instdnceof keyword. 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 valuebreak;} $ 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)

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.