3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Hoge { const CONST_FOO = 'foo'; public function __construct() { // 定数が存在する場合 print constant('self::CONST_FOO') . PHP_EOL; // PHP8以降では定数が存在しない場合は例外が送出される try { print constant('self::NOT_EXIST'); } catch (Error $e) { print $e->getMessage(); print $e->getTraceAsString(); } } } $obj = new Hoge();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
foo Undefined constant self::NOT_EXIST#0 /in/tbFJR(14): constant('self::NOT_EXIST') #1 /in/tbFJR(22): Hoge->__construct() #2 {main}
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 foo Undefined constant self::NOT_EXIST#0 /in/tbFJR(14): constant('self::NOT_EXIST') #1 /in/tbFJR(22): Hoge->__construct() #2 {main}
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
foo Warning: constant(): Couldn't find constant self::NOT_EXIST in /in/tbFJR on line 14

preferences:
167.67 ms | 402 KiB | 181 Q