note 103205 modified in language.operators.type by danbrown

From: Date: Thu, 31 Mar 2011 15:19:59 +0000
Subject: note 103205 modified in language.operators.type by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-178387@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. <?php /** * @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_object($class)) $class=get_class($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); } } return false; } ?> --was-- 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_object($class)) $class=get_class($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); } } return false; } http://php.net/manual/en/language.operators.type.php

« previous php.notes (#178387) next »