3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A { private $priv; function setPrivA( $x ) { $this->priv = $x; } } class B extends A { private $priv; function setPrivB($foo) { $this->priv = $foo; } } class C extends A { function accessOther( $a ) { return $a->priv; } } $b = new B; $b->setPrivA('foo'); $b->setPrivB('bar'); var_dump( $b ); $c = new C; $c->priv = 'baz'; var_dump( $c ); var_dump( $c->accessOther( $b ) );
Output for 8.2.0 - 8.2.16, 8.3.0 - 8.3.3
object(B)#1 (2) { ["priv":"A":private]=> string(3) "foo" ["priv":"B":private]=> string(3) "bar" } Deprecated: Creation of dynamic property C::$priv is deprecated in /in/nq2k5 on line 31 object(C)#2 (2) { ["priv":"A":private]=> NULL ["priv"]=> string(3) "baz" } Fatal error: Uncaught Error: Cannot access private property B::$priv in /in/nq2k5:21 Stack trace: #0 /in/nq2k5(33): C->accessOther(Object(B)) #1 {main} thrown in /in/nq2k5 on line 21
Process exited with code 255.
Output for 8.1.0 - 8.1.27
object(B)#1 (2) { ["priv":"A":private]=> string(3) "foo" ["priv":"B":private]=> string(3) "bar" } object(C)#2 (2) { ["priv":"A":private]=> NULL ["priv"]=> string(3) "baz" } Fatal error: Uncaught Error: Cannot access private property B::$priv in /in/nq2k5:21 Stack trace: #0 /in/nq2k5(33): C->accessOther(Object(B)) #1 {main} thrown in /in/nq2k5 on line 21
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
object(B)#1 (2) { ["priv":"B":private]=> string(3) "bar" ["priv":"A":private]=> string(3) "foo" } object(C)#2 (2) { ["priv":"A":private]=> NULL ["priv"]=> string(3) "baz" } Fatal error: Uncaught Error: Cannot access private property B::$priv in /in/nq2k5:21 Stack trace: #0 /in/nq2k5(33): C->accessOther(Object(B)) #1 {main} thrown in /in/nq2k5 on line 21
Process exited with code 255.

preferences:
170.17 ms | 403 KiB | 166 Q