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.2.36, 8.3.0 - 8.3.37, 8.4.1 - 8.4.28, 8.5.0 - 8.5.13, 8.6.0

preferences:
15.67 ms | 1163 KiB | 6 Q