note 103205 added to language.operators.type

From: Date: Thu, 31 Mar 2011 14:40:59 +0000
Subject: note 103205 added to language.operators.type
Groups: php.notes 
Request: Send a blank email to php-notes+get-178385@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_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; } ---- 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/103205 Del: integrated -- https://master.php.net/note/delete/103205/integrated Del: useless -- https://master.php.net/note/delete/103205/useless Del: bad code -- https://master.php.net/note/delete/103205/bad+code Del: spam -- https://master.php.net/note/delete/103205/spam Del: non-english -- https://master.php.net/note/delete/103205/non-english Del: in docs -- https://master.php.net/note/delete/103205/in+docs Del: other reasons-- https://master.php.net/note/delete/103205 Reject -- https://master.php.net/note/reject/103205 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178385) next »