Home > Backend Development > PHP Tutorial > How to use instanceof

How to use instanceof

不言
Release: 2023-04-02 11:24:02
Original
2633 people have browsed it

这篇文章主要介绍了关于instanceof的使用方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

测试单独一个类:

 1 <?php 
 2  
 3 class A 
 4 {
 5     
 6 } 
 7  
 8 $a = new A(); 
 9 if($a instanceof A){
 10     echo "对象\$a实现了A类";
 11 }
Copy after login

当一个子类继承父类:

 1 <?php 
 2  
 3 class A 
 4 { 
 5      
 6 } 
 7 class B extends A 
 8 { 
 9 
 10 }
 11 $b = new B();
 12 if($b instanceof A){
 13     echo "对象\$b实现了A类";
 14 }
 15 //最终会返回&#39;对象$b实现了A类&#39;;
Copy after login

当实现接口时:

<?php
interface A
{
//这边你可以定义方法
}class B implements A
{
//实现接口的方法
}$b = new B();if($b instanceof A){    echo "对象\$b实现了A类";
}//最终会返回&#39;对象$b实现了A类&#39;;
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

PHPExcel 导入Excel数据的方法

Laravel 学习的基础知识

The above is the detailed content of How to use instanceof. 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