note 103204 deleted from language.operators.type by danbrown
| From: | danbrown@php.net | Date: | Thu, 31 Mar 2011 15:19:47 +0000 |
| Subject: | note 103204 deleted from language.operators.type by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178386@lists.php.net to get a copy of this message | ||
Note Submitter: Jennifer
----
I was getting frustrated by instanceof not taking a string for its first argument so I wrote this
function that takes strings or objects for both args and deals with both classes and interfaces. I
hope it's useful to someone.
/**
* @desc: replacement for instanceof that accept strings or objects for both args
* @param: Mixed $object- string or Object
* @param: Mixed $class- string or Object
* @return: Boolean
*/
function oneof($object, $class){
if(is_object($object)) return $object instanceof $class;
if(is_string($object)){
if(is_string($class)) {
if(class_exists($class)) return is_subclass_of($object, $class) || $object==$class;
if(interface_exists($class)) {
$reflect = new ReflectionClass($object);
return !$reflect->implementsInterface($class);
}
} else if(is_object($class)){
$robj = new ReflectionClass($object);
$rclass=new ReflectionClass($class);
return $r->isInstance($object);
}
}
return false;
}