3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Foo { public const CONSTANT = 1; public function bar(): int { return Foo::CONSTANT; // Fatal error } } class Bar { use Foo; } var_dump(Bar::CONSTANT); // 1 $bar = new Bar(); $bar->bar();
Output for 8.2.22 - 8.2.30, 8.3.5 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
int(1) Fatal error: Uncaught Error: Cannot access trait constant Foo::CONSTANT directly in /in/11of1:9 Stack trace: #0 /in/11of1(20): Bar->bar() #1 {main} thrown in /in/11of1 on line 9
Process exited with code 255.
Output for 8.1.30 - 8.1.34
Fatal error: Traits cannot have constants in /in/11of1 on line 5
Process exited with code 255.

preferences:
53.77 ms | 737 KiB | 4 Q