is_a() for non-instantiated classes?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'd like to be able to instantiate a class from user input.  eg. $a =
new $_GET['var']();.  I can test $_GET['var'] against a whitelist to
make sure that only appropriate classes can be instantiated, but what
I'd really like to be able to do is to see if not only class_exists
($_GET['var']) but also to see if the $_GET['var'] class extends
another class.

The is_a() function will return true if an already instantiated object
is of a given class or is extended from a given class, but I don't
want to instantiate an object before knowing if it's of the
appropriate type - I only want to do it after-the-fact.  The main
reason being that I don't want untrusted users instantiating
inappropriate classes and don't want them running the constructors for
classes that they shouldn't be calling in the first place.

I'm actually going to guess this isn't possible, but maybe I'm wrong?

Re: is_a() for non-instantiated classes?

Quoted text here. Click to load it

What you want is possible, but why would you want to?  It sounds
like you should rework your design.  There should be no reason to
have user input directly determine which classes are instantiated.  
Usually, it's better to have some logic dedicated to parsing user
input and giving you the results you need to make decisions in the

If you really need reflection, it's available:


$email = str_replace('sig.invalid', '', $from);

Anonymous (1984 IOCCC winner):
int i;main()"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p)

Re: is_a() for non-instantiated classes?


You can use something like:


class a {}
class b extends a {}
class c extends b {}

function isSubClass($class, $parent)
  try {
    $c = new ReflectionClass($class);
    $p = new ReflectionClass($parent);
    return $c->isSubclassOf($p);
  } catch (ReflectionException $e) {}
  return false;

echo isSubClass('c', 'a');


Quoted text here. Click to load it

Re: is_a() for non-instantiated classes?

I see Curtis Dyer already answered.. Yes reflection is the way to go!

Quoted text here. Click to load it

Site Timeline