Lab 07: Reflection & Code Gen
Overview
Step 1: ReflectionClass Basics
<?php
class UserService {
public string $name = 'UserService';
protected int $version = 2;
private array $cache = [];
public function __construct(private readonly string $dsn) {}
public function findById(int $id): ?array { return null; }
public function findAll(int $limit = 100, int $offset = 0): array { return []; }
protected function buildQuery(string $table): string { return "SELECT * FROM {$table}"; }
private function connect(): void {}
}
$rc = new ReflectionClass(UserService::class);
echo "=== ReflectionClass ===\n";
echo "Name: " . $rc->getName() . "\n";
echo "Short name: " . $rc->getShortName() . "\n";
echo "File: " . ($rc->getFileName() ?: 'internal') . "\n";
echo "Abstract: " . ($rc->isAbstract() ? 'yes' : 'no') . "\n";
echo "Final: " . ($rc->isFinal() ? 'yes' : 'no') . "\n";
echo "\n=== Methods ===\n";
foreach ($rc->getMethods() as $method) {
$visibility = match(true) {
$method->isPublic() => 'public',
$method->isProtected() => 'protected',
$method->isPrivate() => 'private',
};
printf(" %-12s %s(%s)\n",
$visibility,
$method->getName(),
implode(', ', array_map(fn($p) => '$' . $p->getName(), $method->getParameters()))
);
}
echo "\n=== Properties ===\n";
foreach ($rc->getProperties() as $prop) {
$visibility = match(true) {
$prop->isPublic() => 'public',
$prop->isProtected() => 'protected',
$prop->isPrivate() => 'private',
};
printf(" %-12s %s\n", $visibility, $prop->getName());
}
echo "\n=== Constructor Parameters ===\n";
foreach ($rc->getConstructor()->getParameters() as $param) {
$type = $param->hasType() ? $param->getType()->getName() : 'mixed';
$default = $param->isOptional() ? ' = ' . var_export($param->getDefaultValue(), true) : '';
echo " {$type} \${$param->getName()}{$default}\n";
}Step 2: PHP 8 Attributes
Step 3: ReflectionMethod::invoke & Closures
Step 4: Code Generation with eval()
Step 5: Dynamic Proxy Pattern
Step 6: Attribute-Based ORM Schema Builder
Step 7: Minimal DI Container
Step 8: Capstone — Full Attribute-Driven Framework
Summary
Feature
API
Use Case
Last updated
