3v4l.org

run code in 300+ PHP versions simultaneously
<?php #[Attribute(Attribute::TARGET_CLASS_CONSTANT)] final class SuitInfo { public function __construct(public readonly string $symbol, public readonly string $color) { } } trait EnumDetailsTrait { final protected function getAttribute(string $className): mixed { $reflection = new ReflectionClassConstant($this::class, $this->name); $classAttributes = $reflection->getAttributes($className); return match (count($classAttributes)) { // Return a blank details 0 => new $className(), // Return the provided information 1 => reset($classAttributes)->newInstance(), // PHP doesn't enforce not-repeatable for attributes, so we have to default => throw new RuntimeException(sprintf('%1$s cases must have a single attribute of %2$s ', self::class, $className)), }; } } enum Suit: string { use EnumDetailsTrait; #[SuitInfo('♣', 'black')] case Clubs = 'clubs'; #[SuitInfo('♦', 'red')] case Diamonds = 'diamonds'; #[SuitInfo('♥', 'red')] case Hearts = 'hearts'; #[SuitInfo('♠', 'black')] case Spades = 'spades'; public function getSymbol(): string { return $this->getAttribute(SuitInfo::class)->symbol; } public function getColor() : string { return $this->getAttribute(SuitInfo::class)->color; } } class Card { public function __construct(public readonly string $value, public readonly Suit $suit){} } $myCard = new Card('Q', Suit::Clubs); echo $myCard->suit->getSymbol(); echo PHP_EOL; echo $myCard->suit->getColor();

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.4.140.0120.00917.52
8.4.130.0100.00417.80
8.4.120.0140.00520.70
8.4.110.0100.01018.90
8.4.100.0140.00817.74
8.4.90.0140.00717.92
8.4.80.0140.00517.91
8.4.70.0140.00617.93
8.4.60.0140.00718.79
8.4.50.0140.00619.77
8.4.40.0140.00719.48
8.4.30.0130.00618.95
8.4.20.0140.00717.75
8.4.10.0110.00022.09
8.3.270.0110.00816.35
8.3.260.0050.00416.50
8.3.250.0130.00619.14
8.3.240.0120.00616.97
8.3.230.0090.00716.48
8.3.220.0100.00816.46
8.3.210.0090.01016.82
8.3.200.0050.00316.66
8.3.190.0110.01019.04
8.3.180.0110.00816.60
8.3.170.0070.01117.30
8.3.160.0090.00918.36
8.3.150.0070.01117.29
8.3.140.0050.00317.13
8.3.130.0040.00418.41
8.3.120.0060.00318.95
8.3.110.0000.00918.51
8.3.100.0040.00418.34
8.3.50.0000.00918.56
8.2.290.0040.00520.39
8.2.280.0090.00918.14
8.2.270.0130.00316.81
8.2.260.0040.00419.07
8.2.250.0130.00718.59
8.2.240.0100.00019.03
8.2.230.0130.00718.34
8.2.220.0160.00318.35
8.1.330.0120.00622.12
8.1.320.0120.00716.34
8.1.310.0070.00716.81
8.1.300.0090.00020.07
8.1.100.0060.00217.36

preferences:
29.56 ms | 403 KiB | 5 Q