php 中instanceof的使用

转载:https://www.cnblogs.com/tengjian/p/7999107.html

作用:(1)判断一个对象是否是某个类的实例,(2)判断一个对象是否实现了某个接口。

第一种用法:

<?php
$obj = new A();
if ($obj instanceof A) {
  echo 'A';
}

第二种用法:

<?php
interface 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';
} 
?>
输出结果:Yes, it is