PHP Reference ยท Functions and Classes
PHP Reflection API
Reference for the PHP Reflection API: ReflectionClass, ReflectionMethod, ReflectionProperty, ReflectionFunction, parameters, types and attributes.
Reflection lets a running PHP program inspect itself: the methods of a class, the types of a function's parameters, the attributes attached to a property. Dependency-injection containers, test frameworks, serializers and routers are built on it.
The API is a set of classes. ReflectionClass describes a class, interface, trait or enum; ReflectionObject an instance; ReflectionMethod, ReflectionProperty and ReflectionClassConstant the members; ReflectionFunction a function or closure; ReflectionParameter and ReflectionNamedType the signatures. It is part of PHP core and always available.
$class = new ReflectionClass(Invoice::class); $class->getMethods();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
new ReflectionClass(object|string $objectOrClass) new ReflectionObject(object $object) new ReflectionMethod(object|string $objectOrMethod, ?string $method = null) new ReflectionProperty(object|string $class, string $property) new ReflectionFunction(Closure|string $function) new ReflectionEnum(object|string $objectOrClass)
| Common method | Description |
|---|---|
ReflectionClass::getMethods(), getProperties(), getConstants() |
Arrays of members (filterable by modifier flags). |
getParentClass(), getInterfaceNames(), getTraitNames() |
Hierarchy information. |
newInstance(...$args), newInstanceArgs(array), newInstanceWithoutConstructor() |
Create objects. |
ReflectionMethod::invoke($object, ...$args) |
Call a method, including private ones. |
ReflectionProperty::getValue() / setValue() |
Read or write, including private properties. |
getAttributes() |
List ReflectionAttribute objects; newInstance() builds the attribute class. |
ReflectionNamedType::getName(), allowsNull(), isBuiltin() |
Type details; union types give ReflectionUnionType. |
Errors: a missing class, method or property throws ReflectionException.
Usage notes
- Visibility is bypassed. Since PHP 8.1 private and protected members can be read, written and invoked without
setAccessible(true), which is now a no-op. This is powerful: use it for tools and tests, not for application logic. - Reflection is slower than normal code. Cache results (a container should reflect each class once).
- Types are objects.
$p->getType()can benull(no type), aReflectionNamedType,ReflectionUnionTypeorReflectionIntersectionType. Cast to string for display. - Auto-wiring reads constructor parameter types with
getConstructor()?->getParameters()and builds each dependency. - Attributes are read lazily; the attribute class is only instantiated by
newInstance(). - Doc comments are available through
getDocComment()when OPcache does not strip them. - Enums use
ReflectionEnumandgetCases(). - Not for hot paths. Prefer
instanceof,method_exists()or$object::classwhen they are enough.
Related
class_exists(),method_exists(),get_class(), Attributes,enum- Lessons: Reflection, Attributes