overhead of using Reflection?

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

Threaded View

I have the following code:

            $rc = new ReflectionClass($this->getController());
                    $controller = $rc->newInstance();
                    $method = $rc->getMethod($this->getAction());
                    throw new Exception('Action not found');
                throw new Exception('Controller found but doesnt
implement interface');

despite working as expected, the exact same functionality could be
achieved using class_implements(), get_class_methods() and

Clearly there is no need to instantiate Reflection classes and I only
did it this way because its more elegant, but I'm wondering whether
the overhead of the Reflection-based approach is worth it at all?

Re: overhead of using Reflection?

Quoted text here. Click to load it

Just out of curiousity, why are you doing implementsInterface, and
then hasMethod?  Surely if your class implements the interface you
expect then it can be taken as read that it has that method?

Only way to say which is faster is to build both versions, run them
through a code profiler and see what happens.  I suspect that it would
be faster without reflection though.

Re: overhead of using Reflection?

Quoted text here. Click to load it

Thanks for the reply, the reason I check both for the interface and
for the method is because this is the code for a Front Controller's run
() method.  I need to check that the controller in fact implements the
controller interface (and has indexAction() method, for example), but
the action being called could be anything else.  Granted, if it
hasMethod(), it should be safe to invoke anyway but I thought it
prudent to make sure its in fact a controller class.


Site Timeline