Created
January 27, 2017 23:34
-
-
Save hashar/a8dbb3bee8a0272350320136e9566100 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Eval is https://3v4l.org/TZk8B
Another similar case is https://3v4l.org/QKTGW
Ref: https://phabricator.wikimedia.org/T156364#2977719