3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface factory { public static function new(): factory; } #[Value] class Value { public int $value; public function __construct(int $value) { $this->value = $value; } } #[Value(value: 42)] class One implements factory { public static function new(): One { return new One; } } #[Value(value: 2)] class Two implements factory { public static function new(): Two { return new Two; } } #[Value(value: 3)] class Three implements factory { public static function new(): Three { return new Three; } } $arrayOfClasses = [One::new(), Two::new(), Three::new()]; function dumpAttributeData(ReflectionClass $reflection) { $attributes = $reflection->getAttributes(); foreach ($attributes as $attribute) { var_dump($attribute->getName()); var_dump($attribute->getArguments()); } } foreach ($arrayOfClasses as $class) { dumpAttributeData(new ReflectionClass($class::class)); }
Output for 8.0.0 - 8.0.2, 8.1.23 - 8.1.34, 8.2.10 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
string(5) "Value" array(1) { ["value"]=> int(42) } string(5) "Value" array(1) { ["value"]=> int(2) } string(5) "Value" array(1) { ["value"]=> int(3) }
Output for 7.4.0 - 7.4.15
Fatal error: Cannot use ::class with dynamic class name in /in/dt8R3 on line 62
Process exited with code 255.
Output for 7.3.0 - 7.3.27
Parse error: syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/dt8R3 on line 11
Process exited with code 255.

preferences:
70.5 ms | 1073 KiB | 4 Q