3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private const string PREFIX = "Hello. "; private static string $prefix = "Hello, "; private static function prefix(): string { return "Hello, "; } public function greetFunc(string $to): string { return static::prefix() . $to . "!"; // passes, but insecure } public function greetVar(string $to): string { return static::$prefix . $to . "!"; // passes, but insecure } public function greetConst(string $to): string { return static::PREFIX . $to . "!"; // crashes immediately } } class Bar extends Foo { } echo (new Bar())->greetFunc("World") . PHP_EOL; echo (new Bar())->greetVar("World") . PHP_EOL; // echo (new Bar())->greetConst("World") . PHP_EOL; // Error! Referencing undefined Bar::PREFIX... class Buz extends Foo { protected const string PREFIX = "Hello. "; protected static string $prefix = "Hi, "; protected static function prefix(): string { return "Hi, "; } } echo (new Buz())->greetFunc("World") . PHP_EOL; // No! private function was actually overridden!! echo (new Buz())->greetVar("World") . PHP_EOL; // No! private property was actually overridden!! echo (new Buz())->greetConst("World") . PHP_EOL;
Output for 8.3.0 - 8.3.28, 8.4.1 - 8.4.15, 8.5.0
Hello, World! Hello, World! Hi, World! Hi, World! Hello. World!
Output for 8.2.0 - 8.2.29
Parse error: syntax error, unexpected identifier "PREFIX", expecting "=" in /in/8Trt8 on line 5
Process exited with code 255.

preferences:
44.77 ms | 407 KiB | 5 Q