dna*_*irl 2 php oop reflection
我想确定一个类是否存在以及它是否实现了一个接口.以下两个都应该有效.哪个应该是首选,为什么?
//检查类是否存在,实例化它并找出它是否实现了Annotation
if(class_exists($classname)){
$tmp=new $classname;
if($obj instanceof Annotation) {//do something}
}
Run Code Online (Sandbox Code Playgroud)
//检查类是否存在,对其进行反射并找出它是否实现了Annotation
if(class_exists($classname)){
$r=new new ReflectionClass($classname);
if($r->implementsInterface('Annotation)) {//do something}
}
Run Code Online (Sandbox Code Playgroud)
Gor*_*don 10
看看这些功能
我宁愿在Reflection课堂上对这些课程或实例进行内省.Reflection API用于逆向工程类.
也有一些像其他的Userful的本地函数的interface_exists或property_exists,等等.