I want to determine if a class exists and whether or not it implements an interface. Both of the below should work. Which should be preferred and why?
//check if class exists, instantiate it and find out if it implements Annotation
if(class_exists($classname)){
$tmp=new $classname;
if($obj instanceof Annotation) {//do something}
}
//check if class exists, make a reflection of it and find out if it implements Annotation
if(class_exists($classname)){
$r=new new ReflectionClass($classname);
if($r->implementsInterface('Annotation)) {//do something}
}
Check out these functions
I’d prefer these over the
Reflectionclass for introspection of a class or instance thereof. The Reflection API is for reverse-engineering classes.There is also a number of other userful native function like interface_exists or property_exists, etc.