M
M
MIK Ek2017-04-24 16:20:48
PHP
MIK Ek, 2017-04-24 16:20:48

How to check if an interface exists in PHP without creating an instance?

The class name comes to the method, it is necessary to check this class for the implementation of the interface with minimal effort. Right now I'm doing (new $class) instanceof $interface . But it seems to me a bit unreasonable to create an instance of a class just to test the interface. Moreover, I do not want to create a Reflection object because it looks even worse. Any ideas how to make it less time consuming for the system?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MIK Ek, 2017-04-25
@MIKEk8

interface MyInterface { }
class MyClass implements MyInterface { }
$interfaces = class_implements('MyClass');
if($interfaces && in_array('MyInterface', $interfaces)) {
// Class MyClass implements interface MyInterface
}
Thanks adamsafr for the answer :

Fits?
stackoverflow.com/questions/20169805/php-check-if-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question