Home Backend Development PHP Problem php instanceof what does it mean?

php instanceof what does it mean?

May 14, 2021 am 09:07 AM
instanceof

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";
Copy after login

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 &#39;$m是类test的实例!<br />&#39;; // get this value
switch ($a instanceof test){
  case true :
    echo &#39;YES<br />&#39;;
    break;
  case false :
    echo &#39;No<br />&#39;; //get this value
    break;
}
$d=new testChilern();
if($d instanceof test)echo &#39;$d是类test的子类!<br />&#39;; // get this value
Copy after login

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:

<?php
$obj = new A();
if ($obj instanceof A) {
  echo &#39;A&#39;;
}
Copy after login

Second usage:

<?php
interface ExampleInterface
{
   public function interfaceMethod();
 }
 class ExampleClass implements ExampleInterface
{
   public function interfaceMethod()
   {
     return &#39;Hello World!&#39;;
   }
 }
$exampleInstance = new ExampleClass();
 if($exampleInstance instanceof ExampleInterface){
   echo &#39;Yes, it is&#39;;
 }else{
   echo &#39;No, it is not&#39;;
} 
?>
Copy after login

Output result: Yes, it is

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

<?php
class Foo {
   public $foobar = &#39;Foo&#39;;
   public function test() {
     echo $this->foobar . "\n";
   }
 }
 class Bar extends Foo {
   public $foobar = &#39;Bar&#39;;
 }
$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, &#39;Foo&#39;)); // FALSE
var_dump(is_subclass_of($b, &#39;Foo&#39;)); // TRUE
echo "subclass of Bar\n";
var_dump(is_subclass_of($a, &#39;Bar&#39;)); // FALSE
var_dump(is_subclass_of($b, &#39;Bar&#39;)); // FALSE
?>
Copy after login

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)
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does instanceof do? What does instanceof do? Nov 14, 2023 pm 03:50 PM

The function of instanceof is to determine whether an object is an instance of a certain class, or whether it implements a certain interface. instanceof is an operator used to check whether an object is of a specified type. Usage scenarios of instanceof operator: 1. Type checking: can be used to determine the specific type of an object, so as to perform different logic according to different types; 2. Interface judgment: can be used to determine whether an object implements an interface, so as to determine whether an object implements an interface. The definition of the interface calls the corresponding method; 3. Downward transformation, etc.

How to use instanceof operator in java How to use instanceof operator in java May 19, 2023 am 08:16 AM

Concept 1. This operator is used to operate on objects and check whether the object is of a specific type (type or interface type). Format 2. If the object pointed to by the variable on the left side of the calculator is an object of the class or interface on the right side of the operator, the result is true. (Objectreferencevariable)instanceof(class/interfacetype) instance packagecom.verify_instanceof;publicclassTestInstanceOf{publicstaticvoidmain(String[]args){//The following four lines of code are used to prove: instanceof

What does instanceof mean? What does instanceof mean? Nov 20, 2023 pm 02:32 PM

instanceof is an operator in JavaScript, used to detect whether the "prototype" attribute of the constructor appears anywhere in the prototype chain of the object. The syntax is "object instanceof constructor", where object is the object to be detected and constructor is the object to be detected. Constructor to check.

What does instanceof mean in java What does instanceof mean in java Nov 13, 2023 pm 01:52 PM

In Java, instanceof is a binary operator used to check whether an object is an instance of a class or an instance of a subclass of a class. Its syntax is "object instanceof class", where object is an object Quote, class is a class name or interface name.

Why will an error be reported without adding instanceof? Why will an error be reported without adding instanceof? Nov 13, 2023 pm 03:05 PM

The reason is: The instanceof operator is used to check whether an object is an instance of a specific class (or its derived class). If the object is not an instance of a class, then type determination cannot be made and an error is thrown. To avoid this error, when using the instanceof operator, you need to ensure that the object is an instance of a class. If you are not sure about the type of an object, you can use other methods to determine the type.

instanceof operator in Java instanceof operator in Java Sep 01, 2023 pm 08:01 PM

This operator is only used for object reference variables. This operator checks whether an object belongs to a specific type (class type or interface type). The instanceof operator is written as -(Objectreferencevariable)instanceof(class/interfacetype) If the object referenced by the variable on the left side of the operator passes the IS-A check of the class/interface type on the right side, the result will be true. Here is an example - Example Live Demonstration publicclassTest{ publicstaticvoidmain(Stringargs[]){&nbs

Why not use instanceof Why not use instanceof Nov 14, 2023 pm 04:05 PM

The reasons for not using instanceof are: 1. The programming language you are using may not support the instanceof operator; 2. You think that using other methods can better achieve the requirements. In some cases, using other methods to check the object type may be more effective. or more suitable for your needs; 3. Not familiar with how the instanceof operator is used or unsure of its behavior; 4. In some cases, using "instanceof" may not be the best choice.

Why do we need to force transfer after instanceof? Why do we need to force transfer after instanceof? Nov 14, 2023 pm 03:43 PM

When using the instanceof operator to check the type of an object, if the result is true, it means that the object is an instance of the specified type. However, the compiler does not automatically convert the object to the specified type, so a cast is required. Casting is the operation of converting an object from one type to another. After using the instanceof operator, if you determine that the object is an instance of the specified type and want to operate with that type, you need to perform cast type conversion.

See all articles