Modern PHP 8.x & Internals
5-8 Years (Senior)
Medium
Production Scenario
Explain Reflection vs. Attributes in PHP 8.x and how to build high-performance metadata routers.
Real-World Scenario / Incident Description
Migrating a custom framework from legacy docblock annotations (`@Route("/api/users")`) to native PHP 8 Attributes (`#[Route("/api/users")]`).
401 views
•
Updated 1 month ago
Creating & Reading Native Attributes
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
class Route
{
public function __construct(
public string $path,
public string $method = 'GET'
) {}
}
class UserController
{
#[Route('/api/v1/users', method: 'GET')]
public function list(): JsonResponse { ... }
}
// Reading via Reflection
$reflection = new ReflectionMethod(UserController::class, 'list');
$attributes = $reflection->getAttributes(Route::class);
foreach ($attributes as $attr) {
$routeInstance = $attr->newInstance();
echo $routeInstance->path; // '/api/v1/users'
}
Native attributes are parsed at compile time, eliminating regex docblock string parsing.
Related Concepts:
#Attributes
#Reflection
#Metadata
#Routing
More Modern PHP 8.x & Internals Interview Questions
How does the Zend Engine manage memory with Zvals, Reference Counting, and the Circular Garbage Collector in PHP 8.x?
Read →
Explain PHP 8.4 Asymmetric Visibility, Property Hooks, and Constructor Property Promotion.
Read →
How does the JIT (Just-In-Time) compiler in PHP 8.x work, and why does it benefit CPU-heavy tasks but not typical database-heavy web apps?
Read →
How do PHP Fibers work under the hood, and how do they differ from full async threads (Go Goroutines / Node.js Event Loop)?
Read →
How do you stream and parse a 50GB JSON/CSV export file in PHP without exceeding a 128MB memory limit?
Read →