Checking if an instance's class implements an interface?

前端 未结 6 1185
误落风尘
误落风尘 2020-12-12 21:09

Given a class instance, is it possible to determine if it implements a particular interface? As far as I know, there isn\'t a built-in function to do this directly. What opt

6条回答
  •  暖寄归人
    2020-12-12 22:05

    interface IInterface
    {
    }
    
    class TheClass implements IInterface
    {
    }
    
    $cls = new TheClass();
    if ($cls instanceof IInterface) {
        echo "yes";
    }
    

    You can use the "instanceof" operator. To use it, the left operand is a class instance and the right operand is an interface. It returns true if the object implements a particular interface.

提交回复
热议问题