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); }

preferences:
30.27 ms | 405 KiB | 5 Q