Lab 06: Reflection API
Step 1: ReflectionClass Basics
<?php
class UserService {
public function __construct(
private readonly string $name,
private int $age = 30
) {}
public function greet(): string { return 'Hello ' . $this->name; }
#[\Deprecated('Use greet() instead')]
public function hello(): string { return $this->greet(); }
}
$rc = new ReflectionClass(UserService::class);
echo "Class: " . $rc->getName() . "\n";
echo "Short name: " . $rc->getShortName() . "\n";
echo "Abstract: " . ($rc->isAbstract() ? 'yes' : 'no') . "\n";
echo "Final: " . ($rc->isFinal() ? 'yes' : 'no') . "\n";
echo "\nMethods:\n";
foreach ($rc->getMethods() as $method) {
$vis = $method->isPublic() ? 'public' : ($method->isProtected() ? 'protected' : 'private');
echo " $vis {$method->getName()}()\n";
}Step 2: Inspecting Properties
Step 3: Invoking Methods via Reflection
Step 4: Reading Attributes via Reflection
Step 5: Dynamic Proxy Pattern
Step 6: Reflection for Cloning & Deep Copy
Step 7: Constructor Parameter Inspection
Step 8: Capstone — Dependency Injection Container
Summary
Class
Key Methods
Use Case
Last updated
