3v4l.org

run code in 300+ PHP versions simultaneously
<?php class NumberContainer { public int $sum { get => $this->prop1 + $this->prop2 + $this->prop3; } public function __construct( public int $prop1 = 0 { set(int $value) { if ($value + ($this->prop2 ?? 0) + ($this->prop3 ?? 0) > 100) { throw new \InvalidArgumentException("Setting prop1 to {$value} would make the sum exceed 100."); } $this->prop1 = $value; } }, public int $prop2 = 0 { set(int $value) { if (($this->prop1 ?? 0) + $value + ($this->prop3 ?? 0) > 100) { throw new \InvalidArgumentException("Setting prop2 to {$value} would make the sum exceed 100."); } $this->prop2 = $value; } }, public int $prop3 = 0 { set(int $value) { if (($this->prop1 ?? 0) + ($this->prop2 ?? 0) + $value > 100) { throw new \InvalidArgumentException("Setting prop3 to {$value} would make the sum exceed 100."); } $this->prop3 = $value; } }, ) {} } $container = new NumberContainer(); echo "Initial sum: " . $container->sum . PHP_EOL; echo "Setting prop1 to 30..." . PHP_EOL;; $container->prop1 = 30; echo "Current sum: " . $container->sum . PHP_EOL; echo "Setting prop2 to 40..." . PHP_EOL;; $container->prop2 = 40; echo "Current sum: " . $container->sum . PHP_EOL; echo "Setting prop3 to 20..." . PHP_EOL;; $container->prop3 = 20; echo "Current sum: " . $container->sum . PHP_EOL; echo "Trying to set prop1 to 45 (sum would be 105)..." . PHP_EOL; $container->prop1 = 45; echo "Current sum: " . $container->sum . PHP_EOL;
Output for 8.4.10
Initial sum: 0 Setting prop1 to 30... Current sum: 30 Setting prop2 to 40... Current sum: 70 Setting prop3 to 20... Current sum: 90 Trying to set prop1 to 45 (sum would be 105)... Fatal error: Uncaught InvalidArgumentException: Setting prop1 to 45 would make the sum exceed 100. in /in/7AYvF:13 Stack trace: #0 /in/7AYvF(55): NumberContainer->$prop1::set(45) #1 {main} thrown in /in/7AYvF on line 13
Process exited with code 255.

preferences:
151.66 ms | 995 KiB | 7 Q