I need to check if any class extends or implements a specific interface.
Note that the class name is a variable, i.e. there will be no instance of this class.
Users must select a class from the list of classes, and the system must check whether the class implements a specific interface or not. The list of classes is variable (in accordance with the current PHP software), some of these classes can be initialized, while others cannot.
Here is the code I'm using:
function is_instance_of($obj,$cls){
if(is_string($obj))$obj=new $obj();
if(PHP_MAJOR_VERSION>4)return $obj instanceof $cls;
if(PHP_MAJOR_VERSION>3)return is_a($obj,strtolower($cls));
return false;
}
var_dump(is_instance_of('MyClass','IMyInterface'));
var_dump(is_instance_of('Closure','IMyInterface'));
In this last test, the following error appears:
Cheated fatal error: in C: \ Users \ abcdefghijklmn \ debug.php in line XX creation of 'Closure' is not allowed
, :
$obj=new @$obj();: - , /.try{}catch(){} : -'class' instanceof 'class' ( $obj - ): - false
, , ... . , ( - weirdo, HDD;)).
, - , .
: () : -
function is_instance_of($obj,$cls){
if(is_string($obj) || PHP_MAJOR_VERSION>4){
$rc=new ReflectionClass($obj);
return $rc->implementsInterface($cls);
}else{
if(PHP_MAJOR_VERSION>3)return is_a($obj,strtolower($cls));
return false;
}
}