3v4l.org

run code in 300+ PHP versions simultaneously
<?php // https://ru.stackoverflow.com/questions/1444784 class MyExc1 extends \Exception {} class MyExc2 extends \Exception {} class ExcController { private static $map = [ 22 => MyExc1::class, 28 => MyExc2::class, ]; public static function getExceptionClassByCode($curlErr) { return self::$map[$curlErr]; } public static function throwExceptionByCode($curlErr, $msg) { throw new self::$map[$curlErr]($msg); } } // обратите внимание на круглые скобки /* 1 */ throw new (ExcController::getExceptionClassByCode(22)); // throw new (MyExc1); /* 2 */ throw new (ExcController::getExceptionClassByCode(22))('qwerty'); // throw new (MyExc1)('qwerty') /* 3 */ ExcController::throwExceptionByCode(22, 'qwerty'); // идентичный результат
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught MyExc1 in /in/CnoKD:23 Stack trace: #0 {main} thrown in /in/CnoKD on line 23
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Parse error: syntax error, unexpected '(' in /in/CnoKD on line 23
Process exited with code 255.

preferences:
128.73 ms | 401 KiB | 121 Q