[php-src] Issue #11205: Serialization issue with parent class and missing __serialize() method
| From: | SoftCreatR | Date: | Mon, 08 May 2023 09:56:15 +0000 |
| Subject: | [php-src] Issue #11205: Serialization issue with parent class and missing __serialize() method | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-244398@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11205
Author: SoftCreatR
### Description
This issue likely is both a bug report and a feature request, because it appears to be an oversight:
For a class extending an abstract class that is not under my control I needed to control the
serialization format to “flush” a “cache property” either when serializing or unserializing
the class. I wanted to implement this using the recommended
__serialize() and
__unserialize() hooks.
Unfortunately the abstract parent class does not implement these methods and it also contains
important private properties that need to be preserved. As these properties are private, I cannot
access those in my child class and neither should I need to know about their existence. However by
implementing __serialize() and __unserialize() those private properties
will not be preserved and reset to their default value.
An example is showcased in https://3v4l.org/Qdkcs and also
included below:
```php
<?php
class P {
private int $someProp = 0;
public function setProp(int $value): void
{
$this->someProp = $value;
}
}
class C extends P {
private array $cache = [];
private int $someOtherProp;
public function __construct(int $value)
{
$this->someOtherProp = $value;
}
public function fillCache(): void
{
$this->cache[] = 'value';
}
public function __serialize(): array
{
return [
'someOtherProp' => $this->someOtherProp
];
}
public function __unserialize(array $data): void
{
$this->someOtherProp = $data['someOtherProp'];
$this->cache = [];
}
}
$c = new C(7);
$c->setProp(5);
$c->fillCache();
var_dump($c);
var_dump($s = serialize($c));
var_dump(unserialize($s));
?>
```
Should private properties of parent classes be implicitly preserved or a default implementation of
__serialize() be generated that I can call in my child class?
### PHP Version
8.1.18
### Operating System
Ubuntu 22.04