3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CustomException extends Exception { public $object; public $wantedValue; public $changedValue; public function __construct( string $msg, SomeClass $object, int $wantedValue, int $changedValue ) { parent::__construct($msg); $this->object = $object; $this->wantedValue = $wantedValue; $this->changedValue = $changedValue; } } class SomeClass { private $value; public function __construct(int $value) { $this->value = $value; if($value > 10) { throw new CustomException( sprintf("Value %d is larger than the max 10 setting to 10!", $value), $this, $value, $this->value = 10 ); } } } $valid = new SomeClass(9); var_dump($valid); try { $invalid = new SomeClass(20); } catch (CustomException $e) { echo PHP_EOL, $e->getMessage(), PHP_EOL, PHP_EOL; $invalid = $e->object; // I can still access the invalid object var_dump($invalid); }
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
object(SomeClass)#1 (1) { ["value":"SomeClass":private]=> int(9) } Value 20 is larger than the max 10 setting to 10! object(SomeClass)#2 (1) { ["value":"SomeClass":private]=> int(10) }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 object(SomeClass)#1 (1) { ["value":"SomeClass":private]=> int(9) } Value 20 is larger than the max 10 setting to 10! object(SomeClass)#2 (1) { ["value":"SomeClass":private]=> int(10) }

preferences:
118.98 ms | 402 KiB | 184 Q