3v4l.org

run code in 300+ PHP versions simultaneously
<?php class InCommon { private $baz = 'foo'; public function out($pref){ echo "\n".$pref.'::$baz = '.$this->baz; } } class Foo extends InCommon { public function peek($target, $pref){ echo "\n".'peek @'.$pref.'::$baz = '.$target->baz; } } class Bar extends InCommon { public function peek($target, $pref){ echo "\n".'peek @'.$pref.'::$baz = '.$target->baz; } public function mess($target){ $target->baz = 'cats'; } } $foo = new Foo(); $bar = new Bar(); echo "Messing with variable"; $bar->mess($foo); // bar messes with foo's baz, which should be private echo "\nFoo looks at its own $baz:"; $foo->out('Foo'); // foo looks at its own baz, which remains unchanged echo "\nBar looks at Foo's $baz:"; $bar->peek($foo, 'Foo'); // yet when bar looks at foo's baz (which is shouldn't see) we get the change from above echo "\n\nFoo can't look at Bar's $baz"; $foo->peek($bar, 'Bar');
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Messing with variable Deprecated: Creation of dynamic property Foo::$baz is deprecated in /in/c3Q9E on line 17 Warning: Undefined variable $baz in /in/c3Q9E on line 26 Foo looks at its own : Foo::$baz = foo Warning: Undefined variable $baz in /in/c3Q9E on line 28 Bar looks at Foo's : peek @Foo::$baz = cats Warning: Undefined variable $baz in /in/c3Q9E on line 31 Foo can't look at Bar's Warning: Undefined property: Bar::$baz in /in/c3Q9E on line 10 peek @Bar::$baz =
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
Messing with variable Warning: Undefined variable $baz in /in/c3Q9E on line 26 Foo looks at its own : Foo::$baz = foo Warning: Undefined variable $baz in /in/c3Q9E on line 28 Bar looks at Foo's : peek @Foo::$baz = cats Warning: Undefined variable $baz in /in/c3Q9E on line 31 Foo can't look at Bar's Warning: Undefined property: Bar::$baz in /in/c3Q9E on line 10 peek @Bar::$baz =
Output for 5.2.6 - 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.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Messing with variable Notice: Undefined variable: baz in /in/c3Q9E on line 26 Foo looks at its own : Foo::$baz = foo Notice: Undefined variable: baz in /in/c3Q9E on line 28 Bar looks at Foo's : peek @Foo::$baz = cats Notice: Undefined variable: baz in /in/c3Q9E on line 31 Foo can't look at Bar's Notice: Undefined property: Bar::$baz in /in/c3Q9E on line 10 peek @Bar::$baz =
Output for 7.3.32 - 7.3.33
Messing with variable Foo looks at its own : Foo::$baz = foo Bar looks at Foo's : peek @Foo::$baz = cats Foo can't look at Bar's peek @Bar::$baz =
Output for 5.0.4 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.5
Messing with variable Notice: Undefined variable: baz in /in/c3Q9E on line 26 Foo looks at its own : Foo::$baz = foo Notice: Undefined variable: baz in /in/c3Q9E on line 28 Bar looks at Foo's : peek @Foo::$baz = cats Notice: Undefined variable: baz in /in/c3Q9E on line 31 Foo can't look at Bar's Notice: Undefined property: Bar::$baz in /in/c3Q9E on line 10 peek @Bar::$baz =
Output for 5.0.0 - 5.0.3
Messing with variable Notice: Undefined variable: baz in /in/c3Q9E on line 26 Foo looks at its own : Foo::$baz = foo Notice: Undefined variable: baz in /in/c3Q9E on line 28 Bar looks at Foo's : peek @Foo::$baz = cats Notice: Undefined variable: baz in /in/c3Q9E on line 31 Foo can't look at Bar's Notice: Undefined property: Bar::$baz in /in/c3Q9E on line 10 peek @Bar::$baz =
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/c3Q9E 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_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/c3Q9E on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/c3Q9E on line 3
Process exited with code 255.

preferences:
252.63 ms | 401 KiB | 450 Q