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 { set(int $value) { if (isset($this->prop2) && isset($this->prop3) && $value + $this->prop2 + $this->prop3 !== 100) { throw new \InvalidArgumentException("Setting prop1 to {$value} would make the sum be not equals 100."); } $this->prop1 = $value; } }, public int $prop2 { set(int $value) { if (isset($this->prop1) && isset($this->prop3) && $this->prop1 + $value + $this->prop3 !== 100) { throw new \InvalidArgumentException("Setting prop2 to {$value} would make the sum be not equals 100."); } $this->prop2 = $value; } }, public int $prop3 { set(int $value) { if (isset($this->prop1) && isset($this->prop2) && $this->prop1 + $this->prop2 + $value !== 100) { throw new \InvalidArgumentException("Setting prop3 to {$value} would make the sum be not equals 100."); } $this->prop3 = $value; } }, ) {} } echo "Instanciate with 30, 50, 20" . PHP_EOL; $container = new NumberContainer(30, 50, 20); try { echo "Setting prop2 to 666" . PHP_EOL; $container->prop2 = 666; } catch (InvalidArgumentException $e) { echo $e->getMessage() . PHP_EOL; } try { echo "Setting prop3 to 12". PHP_EOL; $container->prop2 = 12; } catch (InvalidArgumentException $e) { echo $e->getMessage() . PHP_EOL; } try { echo "Instanciate with 1337, 42, 3886" . PHP_EOL; $container = new NumberContainer(1337, 42, 3886); } catch (InvalidArgumentException $e) { echo $e->getMessage() . PHP_EOL; }
Output for 8.4.10
Instanciate with 30, 50, 20 Setting prop2 to 666 Setting prop2 to 666 would make the sum be not equals 100. Setting prop3 to 12 Setting prop2 to 12 would make the sum be not equals 100. Instanciate with 1337, 42, 3886 Setting prop3 to 3886 would make the sum be not equals 100.

preferences:
152.8 ms | 995 KiB | 7 Q