note 103204 added to language.operators.type
| From: | Jennifer at osu1 dot php dot net | Date: | Thu, 31 Mar 2011 14:35:03 +0000 |
| Subject: | note 103204 added to language.operators.type | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178384@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 216.235.13.26
Probable Submitter: 132.246.244.119
----
Manual Page -- http://www.php.net/manual/en/language.operators.type.php
Edit -- https://master.php.net/note/edit/103204
Del: integrated -- https://master.php.net/note/delete/103204/integrated
Del: useless -- https://master.php.net/note/delete/103204/useless
Del: bad code -- https://master.php.net/note/delete/103204/bad+code
Del: spam -- https://master.php.net/note/delete/103204/spam
Del: non-english -- https://master.php.net/note/delete/103204/non-english
Del: in docs -- https://master.php.net/note/delete/103204/in+docs
Del: other reasons-- https://master.php.net/note/delete/103204
Reject -- https://master.php.net/note/reject/103204
Search -- https://master.php.net/manage/user-notes.php