3v4l.org

run code in 300+ PHP versions simultaneously
<?php 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' ); $pub_s = serialize( $pub ); $priv = new WithPrivate( 'value' ); $priv_s = serialize( $priv ); echo "Class with public property unserialized to class with private property\n"; $mut_pub_to_priv = unserialize( str_replace( '10:"WithPublic"', '11:"WithPrivate"', $pub_s ) ); var_dump( $mut_pub_to_priv ); echo "Property should be 'value': "; var_export( $mut_pub_to_priv->getProperty() ); echo "\n"; echo "\nClass with private property unserialized to class with public property\n"; $mut_priv_to_pub = unserialize( str_replace( '11:"WithPrivate"', '10:"WithPublic"', $priv_s ) ); var_dump( $mut_priv_to_pub ); echo "Property should be 'value': "; var_export( $mut_priv_to_pub->getProperty() ); echo "\n";
Output for 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
Class with public property unserialized to class with private property object(WithPrivate)#3 (1) { ["property":"WithPrivate":private]=> string(5) "value" } Property should be 'value': valueNULL Class with private property unserialized to class with public property Deprecated: Creation of dynamic property WithPublic::$property is deprecated in /in/QKTGW on line 27 object(WithPublic)#4 (2) { ["property"]=> NULL ["property":"WithPrivate":private]=> string(5) "value" } Property should be '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.30
Class with public property unserialized to class with private property object(WithPrivate)#3 (1) { ["property":"WithPrivate":private]=> string(5) "value" } Property should be 'value': valueNULL Class with private property unserialized to class with public property object(WithPublic)#4 (2) { ["property"]=> NULL ["property":"WithPrivate":private]=> string(5) "value" } Property should be 'value': NULL
Output for 7.2.0 - 7.2.7
Class with public property unserialized to class with private property object(WithPrivate)#3 (1) { ["property":"WithPrivate":private]=> string(5) "value" } Property should be 'value': valueNULL Class with private property unserialized to class with public property object(WithPublic)#4 (1) { ["property"]=> string(5) "value" } Property should be 'value': valueNULL
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33
Class with public property unserialized to class with private property object(WithPrivate)#3 (2) { ["property":"WithPrivate":private]=> NULL ["property"]=> string(5) "value" } Property should be 'value': NULL Class with private property unserialized to class with public property object(WithPublic)#4 (2) { ["property"]=> NULL ["property":"WithPrivate":private]=> string(5) "value" } Property should be 'value': NULL

preferences:
67.75 ms | 412 KiB | 5 Q