NotionCommotion Posted February 12, 2022 Share Posted February 12, 2022 Both of these approaches work, however, I am surprised that there isn't a more direct approach. is_a doesn't appear to work with interfaces and instanceof doesn't work with class names. Is there a more proper way of doing so? If not, is one of my two approaches better than the other (I am assuming the in_array approach is more performant and is more intuitive at least to me). Thanks private function classImplementsInterface1(string $class, string $interface):bool { return in_array($interface, class_implements($class)); } private function classImplementsInterface2(string $class, string $interface):bool { return (new \ReflectionClass($class))->implementsInterface($interface); } Quote Link to comment https://forums.phpfreaks.com/topic/314553-tell-if-class-implements-an-interface/ Share on other sites More sharing options...
requinix Posted February 12, 2022 Share Posted February 12, 2022 is_subclass_of, which continues the tradition of some things using the word "class" to represent interfaces too. Quote Link to comment https://forums.phpfreaks.com/topic/314553-tell-if-class-implements-an-interface/#findComment-1594103 Share on other sites More sharing options...
NotionCommotion Posted February 14, 2022 Author Share Posted February 14, 2022 Thanks, Was going to say "the docs for is_a should list this under see other" but just looked again and see it is already there. Quote Link to comment https://forums.phpfreaks.com/topic/314553-tell-if-class-implements-an-interface/#findComment-1594121 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.