Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties of child classes lost after serialize->unserialize #915

Closed
david0 opened this issue Apr 8, 2015 · 1 comment
Closed

Properties of child classes lost after serialize->unserialize #915

david0 opened this issue Apr 8, 2015 · 1 comment
Labels

Comments

@david0
Copy link
Contributor

david0 commented Apr 8, 2015

An object that is serialized using sleep looses instance properties that are declared in child classes:

class Book extends Base\Book
{
  private $color;
  public function setColor($color) { $this->color = $color; }
  public function getColor() { return $this->color; }
}

$book = new Book();
$book->setColor('blue');

echo 'Before: '.$book->getColor()."\n";
$unserializedBook = unserialize(serialize($book));
echo 'After: '.$unserializedBook->getColor()."\n";

Output

Before: blue
After:

I would expect that the property would be restored by default, similar to PHP's default behavior.
baseObjectMethods.php:209 is causing this problems. get_object_vars is scope aware and does not return the attributes of child classes. ReflectionClass::getProperties should work as expected here.

@marcj
Copy link
Member

marcj commented Apr 8, 2015

Thanks for the report. Do you mind sending a fix in a pull-request?

@marcj marcj added the Bug label Apr 8, 2015
@marcj marcj closed this as completed in d2f8b89 Oct 2, 2015
marcj added a commit that referenced this issue Oct 2, 2015
Fix #915  serialize custom properties in __sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants