3v4l.org

run code in 300+ PHP versions simultaneously
<?php # Acknowledging Stas Malyshev # https://phabricator.wikimedia.org/T156364#2977719 # Given two classes with the same property name but different visibility class WithPublic { public $property; function __construct( $p ) { $this->property = $p; } function getProperty() { print $this->property; } } class WithPrivate { private $property; function __construct( $p ) { $this->property = $p; } function getProperty() { print $this->property; } } $pub = new WithPublic( 'value' ); # Lets pretend it is held in a permanent cache: $cache = serialize( $pub ); # Later on NEW code is deployed which change the property signature to private. # Simulate the class changed: $mut_pub_to_priv = unserialize( str_replace( '10:"WithPublic"', '11:"WithPrivate"', $cache ) ); var_dump( $mut_pub_to_priv ); # class WithPrivate#2 (2) { # private $property => # NULL # public $property => # string(5) "value" # } var_export( $mut_pub_to_priv->getProperty() ); # NULL # The object restored from cache is not quite the one we expected and cause # some havoc ( https://phabricator.wikimedia.org/T156364 ). Then Zend and HHVM # consistently yield "NULL". # The other way around. Cache with private property restore to class with # public property $priv = new WithPrivate( 'value' ); $cache = serialize( $priv ); $mut_priv_to_pub = unserialize( str_replace( '11:"WithPrivate"', '10:"WithPublic"', $cache) ); var_dump( $mut_priv_to_pub ); # class WithPublic#4 (2) { # public $property => # NULL # private $property => # string(5) "value" # } var_export( $mut_priv_to_pub->getProperty() ); # On Zend PHP: NULL # On HHVM 3.15.4, 3.17.1: valueNULL
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
object(WithPrivate)#2 (1) { ["property":"WithPrivate":private]=> string(5) "value" } valueNULL Deprecated: Creation of dynamic property WithPublic::$property is deprecated in /in/TZk8B on line 49 object(WithPublic)#4 (2) { ["property"]=> NULL ["property":"WithPrivate":private]=> string(5) "value" } NULL
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 object(WithPrivate)#2 (1) { ["property":"WithPrivate":private]=> string(5) "value" } valueNULL Deprecated: Creation of dynamic property WithPublic::$property is deprecated in /in/TZk8B on line 49 object(WithPublic)#4 (2) { ["property"]=> NULL ["property":"WithPrivate":private]=> string(5) "value" } NULL
Output for 7.2.8 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
object(WithPrivate)#2 (1) { ["property":"WithPrivate":private]=> string(5) "value" } valueNULLobject(WithPublic)#4 (2) { ["property"]=> NULL ["property":"WithPrivate":private]=> string(5) "value" } NULL
Output for 7.2.0 - 7.2.7
object(WithPrivate)#2 (1) { ["property":"WithPrivate":private]=> string(5) "value" } valueNULLobject(WithPublic)#4 (1) { ["property"]=> string(5) "value" } valueNULL
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33
object(WithPrivate)#2 (2) { ["property":"WithPrivate":private]=> NULL ["property"]=> string(5) "value" } NULLobject(WithPublic)#4 (2) { ["property"]=> NULL ["property":"WithPrivate":private]=> string(5) "value" } NULL

preferences:
201.32 ms | 402 KiB | 207 Q