How to determine whether an object belongs to a class in PHP?

WBOY
Release: 2023-04-10 18:36:01
Original
4415 people have browsed it

In the previous article, I brought you "You must understand what abstract classes and abstract methods are in PHP", which introduced the relevant knowledge of abstract classes and abstract methods in PHP in detail. This article In this article, let’s take a look at how to determine whether an object belongs to a class in PHP. I hope it will be helpful to everyone!

How to determine whether an object belongs to a class in PHP?

In PHP, if you want to determine whether an object belongs to a class, you need to pass instanceof , and 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 performs corresponding operations. This can be used to determine whether an object belongs to a certain class. Its syntax format is as follows:

对象名 instanceof 类名;
Copy after login

Judge whether an object belongs to a certain class

If you want to determine whether an object belongs to a certain class, use the instanceof keyword. According to the syntax format, if the object belongs to this class, it will return true; if it does not belong to this class, it will return false.

Next, let’s take an example to see whether an object belongs to a certain class. The example is as follows:

<?php
    class study{
    }
    class study1{
    }
    $obj = new study;
    var_dump($obj instanceof study);
    echo &#39;<br>&#39;;
    var_dump($obj instanceof study1);
?>
Copy after login

Output result:

How to determine whether an object belongs to a class in PHP?

It can be seen from the above results that the detected object belongs to the study class and returns The result is true. The detected object does not belong to the study1 class, so false is returned. Therefore, instanceof can be used to determine whether an object belongs to a certain class.

Determine whether an object belongs to a subclass of a certain parent class

Through the above cases we have been able to determine whether an object belongs to a class Not only that, instanceof can also be used to determine whether an object inherits from a subclass of a certain parent class. Similarly, if the detected object belongs to a subclass of this parent class, the returned result is true. If the detected object If the object does not belong to a subclass of this parent class, the returned result is false.

Next let’s take a look at an example. The example is as follows:

<?php
    class study{
    }
    class study1 extends study{
    }
    $obj = new study1;
    var_dump($obj instanceof study);
    echo &#39;<br>&#39;;
    var_dump($obj instanceof study1);
?>
Copy after login

Output result:

How to determine whether an object belongs to a class in PHP?

By It can be seen from the above results that instanceof can also be used to determine whether an object is a subclass inherited from a certain parent class.

Determine whether an object belongs to an interface object

We can also use instanceof to determine whether a variable implements an interface The object is the same as the above. If the output result is true, it means that the object belongs to this interface. If the output result is false, it means that the object does not belong to this interface.

Let’s take a look at the example. The example is as follows:

<?php
    interface study{
    }
    class study1 implements study{
    }
    $obj = new study1;
    var_dump($obj instanceof study);
    echo &#39;<br>&#39;;
    var_dump($obj instanceof study1);
?>
Copy after login

Output result:

How to determine whether an object belongs to a class in PHP?

In the above example, through instanceof Determines whether a variable is an object of an interface.

When the object being judged does not exist

There is another situation we need to pay attention to. If the variable being detected is not an object, then return Will there be any changes in the results? Will there be any impact? instanceof will not report an error but will directly return flase.

Let’s take a look at the example. The example is as follows:

<?php
    class study{
    }
    class study1{
    }
    $obj = new study;
    var_dump($obj instanceof test);
    echo &#39;<br>&#39;;
    var_dump($obj instanceof demo);
?>
Copy after login

Output result:

How to determine whether an object belongs to a class in PHP?

As can be seen from the above example, The monitored variable is not an object, and the system does not report an error, but returns false.

If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.

The above is the detailed content of How to determine whether an object belongs to a class in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!