3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); // Would also need the same code for BaseError abstract class BaseException extends Exception { private static $codeReflection; private function setCode(string $code) { if (!isset(self::$codeReflection)) { self::$codeReflection = new ReflectionProperty(self::class, 'code'); self::$codeReflection->setAccessible(true); } self::$codeReflection->setValue($this, $code); } public function __construct(string $message, string $code, \Throwable $previous = null) { parent::__construct($message, 0, $previous); $this->setCode($code); } } class DivisionByZeroException extends BaseException {} $e = new DivisionByZeroException("Test", 'Test001'); var_dump($e->getMessage(), $e->getCode());
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 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.18, 8.3.0 - 8.3.6
string(4) "Test" string(7) "Test001"

preferences:
184.52 ms | 403 KiB | 183 Q