3v4l.org

run code in 500+ PHP versions simultaneously
<?php #[Attribute] class Y { public static function get(string $path): self { return new self($path, 'GET'); } // non public constructor private function __construct( public string $path, public string $method ) {} } #[Y] // Actually, Y::get('/home') class X {} $reflectionClass = new ReflectionClass(X::class); $attributes = $reflectionClass->getAttributes(Y::class); foreach ($attributes as $attribute) { $routeData = $attribute->getArguments(); $route = Y::get('mypath'); // Manually call the named constructor }
Output for 8.2.34, 8.3.0 - 8.3.35, 8.4.1 - 8.4.26, 8.5.0 - 8.5.11

preferences:
26.87 ms | 1151 KiB | 7 Q