Serializing a static doesn't really make sense, so I changed that to a non-static in my answer:
You're supposed to implement Serializable in the parent class as well:
class A implements Serializable
{
public function __construct(private $aVar) {}
public function serialize() {
return serialize($this->aVar);
}
public function unserialize($serialized) {
$this->aVar = unserialize($serialized);
}
}
class B extends A implements Serializable
{
public function __construct($aVar, private $bVar) {
parent::__construct($aVar);
}
public function serialize() {
return serialize([$this->bVar, parent::serialize()]);
}
public function unserialize($serialized) {
$arr = unserialize($serialized);
$this->bVar = $arr[0];
parent::unserialize($arr[1]);
}
}
$obj = new B('aVal', 'bVal');
$ser = serialize($obj);
echo $ser . "\n";
var_export(unserialize($ser));
C:1:"B":44:{a:2:{i:0;s:4:"bVal";i:1;s:11:"s:4:"aVal";";}}
B::__set_state(array(
'aVar' => 'aVal',
'bVar' => 'bVal',
))
If you can't do that, you will have to use reflection to get and set the value of aVar from B. That's not an elegant solution, though.
And I know this is an old question, but the Serializable interface is deprecated, you should use __serialize() and __unserialize() instead. This work much the same way, just remove the interface, and you don't need to call \serialize()/\unserialize() in your methods. Also, all classes must serialize as arrays:
// in class A:
public function __serialize() {
return [$this->aVar];
}
public function __unserialize($serialized) {
list($this->aVar) = $serialized;
}
// in class B:
public function __serialize() {
return [$this->bVar, parent::__serialize()];
}
public function __unserialize($serialized) {
$this->bVar = $serialized[0];
parent::__unserialize($serialized[1]);
}