Checking if an instance's class implements an interface?

前端 未结 6 1187
误落风尘
误落风尘 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:04

    You can also do the following

    public function yourMethod(YourInterface $objectSupposedToBeImplementing) {
       //.....
    }
    

    It will throw an recoverable error if the $objectSupposedToBeImplementing does not implement YourInterface Interface.

提交回复
热议问题