Reflection and Introspection in PHP: When Code Looks at Itself
\n\nImagine writing a program that can not only perform actions but also analyze itself. Sounds like science fiction? It's not — in PHP this is called reflection and introspection. These mechanisms allow your code to "know" its own structure: which classes are loaded, what methods an object has, what arguments a function accepts.
\n\nIf you've ever wondered how popular PHP frameworks (Laravel, Symfony) or autoloading systems work — the answer lies right here. Today we'll break down reflection in PHP with real examples so you can use it in your projects. Let's go!
\n\nWhat Are Reflection and Introspection in PHP?
\n\nLet's set the record straight. In PHP, these two terms are often used interchangeably, but there's a subtle difference:
\n\n- \n
- Introspection — is the ability of an object to "look inside itself": find out the class name, list of methods, properties. Example:
get_class($obj)ormethod_exists($obj, 'method'). \n - Reflection — is a more powerful mechanism that allows not only looking but also changing code behavior at runtime: creating objects without
new, calling private methods, retrieving annotations from docblocks. \n
To put it simply: introspection in PHP is "look", and reflection is "look and touch". For reflection in PHP, there is a special set of classes — the Reflection API.
\n\nWhy Do You Need This? Three Real-World Scenarios
\n\nBefore diving into code, let's answer the question: "Will I ever need this?". Spoiler: yes, if you:
\n\n- \n
- Write your own ORM or form validator; \n
- Want to automatically document an API; \n
- Build a plugin or middleware system; \n
- Work with DI containers (Dependency Injection). \n
Without PHP reflection, frameworks like Symfony or Laravel simply couldn't automatically inject dependencies into constructors. But let's take it step by step.
\n\nSimple Introspection Tools: Without the Reflection API
\n\nLet's start with the basics. PHP provides several built-in functions for introspection. They don't require loading classes and work instantly.
\n\nDetermining Type and Class
\n\n<?php\nclass User {\n public $name;\n private $email;\n}\n\n$user = new User();\n\n// Introspection: find out the object's class\necho get_class($user); // User\n\n// Check if a property exists\nvar_dump(property_exists($user, 'email')); // true\n\n// Check if a method exists\nvar_dump(method_exists($user, 'getName')); // false\n?>\n\nThese functions are your first helpers. But what if you need to know the access modifier of a property (public, private, protected)? Simple introspection falls short here — that's when ReflectionClass comes into play.
\n\nReflection API: The Full Power of Reflection in PHP
\n\nThe Reflection API in PHP is a set of classes (ReflectionClass, ReflectionMethod, ReflectionProperty, ReflectionParameter, and others) that allow you to work with code as data. Let's break down the most useful ones.
\n\nReflectionClass: The Anatomy of a Class
\n\nWith ReflectionClass, you can learn literally everything about a class:
<?php\nclass Product {\n public $title;\n protected $price;\n private $secret;\n\n public function __construct($title, $price) {\n $this->title = $title;\n $this->price = $price;\n }\n\n public function getPrice() {\n return $this->price;\n }\n}\n\n$reflection = new ReflectionClass('Product');\n\n// Class name\necho