Home>Article>Backend Development> php instanceof what does it mean?

php instanceof what does it mean?

藏色散人
藏色散人 Original
2021-05-14 09:07:12 5167browse

php instanceof is a keyword in php. You can use the instanceof 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.

php instanceof what does it mean?

The operating environment of this article: Windows 7 system, PHP version 5.6, Dell G3 computer.

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";

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的实例!
'; // get this value switch ($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是类test的子类!
'; // get this value

What is the function of instanceof in php

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

First usage:


      

Second usage:

Output result: Yes, it is

In addition, please pay attention to the difference between instanceof and is_subclass_of(), please see the code:

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 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)

Recommended learning: 《PHP Video Tutorial

The above is the detailed content of php instanceof what does it mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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