🐘 PHP Advanced

What is Reflection in PHP?

Why Interviewers Ask This

Interviewers ask this to evaluate whether you have the depth of knowledge needed to mentor others and lead technical decisions. The expected answer goes beyond definitions into practical implications and real-world consequences.

Answer

PHP's Reflection API allows introspection of classes, interfaces, functions, methods, and properties at runtime. ReflectionClass, ReflectionMethod, ReflectionProperty, and ReflectionFunction are the main classes. Example: $ref = new ReflectionClass($object); $methods = $ref->getMethods(); $props = $ref->getProperties();. Reflection enables reading and instantiating classes from string names, accessing private members (for testing), reading method parameter types and defaults, and checking for attributes (PHP 8) or docblock annotations (older approach). Dependency injection containers use Reflection extensively to automatically resolve constructor dependencies. Reflection is powerful but has a performance cost — use it in bootstrapping/compilation phases rather than hot request paths.

Common Mistake

Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong PHP candidates.