3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class Expression { protected function escapeStringLiteral(string $value): string { return 'never called'; } } trait ExpressionTrait { protected function escapeStringLiteral(string $value): string { return '\'' . str_replace('\'', '\'\'', $value) . '\''; } } class MyExpression extends Expression { use ExpressionTrait; } class TestExpression extends Expression { private Expression $dummyExpression; public function __construct(Expression $dummyExpression) { $this->dummyExpression = $dummyExpression; } #[\Override] protected function escapeStringLiteral(string $value): string { return $this->dummyExpression->escapeStringLiteral($value); } public function test(): void { var_dump($this->escapeStringLiteral('foo \' bar')); } }; class TestCl { public function test(): void { $exprNoRender = new TestExpression(new MyExpression()); $exprNoRender->test(); } } (new TestCl())->test();
Output for 8.2.20 - 8.2.30, 8.3.8 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
string(12) "'foo '' bar'"
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Fatal error: Uncaught Error: Call to protected method MyExpression::escapeStringLiteral() from scope TestExpression in /in/KI9eF:36 Stack trace: #0 /in/KI9eF(41): TestExpression->escapeStringLiteral('foo ' bar') #1 /in/KI9eF(51): TestExpression->test() #2 /in/KI9eF(55): TestCl->test() #3 {main} thrown in /in/KI9eF on line 36
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Call to protected method MyExpression::escapeStringLiteral() from context 'TestExpression' in /in/KI9eF:36 Stack trace: #0 /in/KI9eF(41): TestExpression->escapeStringLiteral('foo ' bar') #1 /in/KI9eF(51): TestExpression->test() #2 /in/KI9eF(55): TestCl->test() #3 {main} thrown in /in/KI9eF on line 36
Process exited with code 255.
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33
Parse error: syntax error, unexpected 'Expression' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/KI9eF on line 26
Process exited with code 255.
Output for 7.0.0 - 7.0.33
Parse error: syntax error, unexpected 'Expression' (T_STRING), expecting variable (T_VARIABLE) in /in/KI9eF on line 26
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/KI9eF on line 5
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected ':', expecting '{' or ';' in /in/KI9eF on line 5
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CLASS in /in/KI9eF on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_CLASS in /in/KI9eF on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/KI9eF on line 3
Process exited with code 255.

preferences:
81.74 ms | 2754 KiB | 4 Q