To give some context; this process was working prior to adding in the Serializable interface. The TestAction2 is not keeping a reference to itself after unserializing; and I have tried adding the Serializable to the Action class and telling it to serialize the $parent but still no difference. It is correctly unserializing the $method field though.
Class Action serializes the reference in $parent field when the serialization is being done on the class that is referencing it (class B), but not when it references itself. Something with serializing happening on Class A or B but not the Action class itself.
class A implements Serializable { private static $label; public function serialize() { return serialize(self::$label); } public function unserialize($serialized) { self::$label = unserialize($serialized); } } class B extends A { private $actions; public function serialize() { return serialize([ 'actions' => $this->actions, 'parent' => parent::serialize()]); } public function unserialize($serialized) { $data = unserialize($serialized); $this->actions = $data['actions']; parent::unserialize($data['parent']); } public function addAction($anAction) { $this->actions[] = $anAction; } public function process() { $this->addAction(new TestAction1($this, 'test1')); // WORKS FINE $this->addAction(new TestAction2($this, 'test2')); // WORKS NOT! } } class Action { private $parent; private $method; public function __construct($cParent, $sMethod) { $this->parent = $cParent; } } class TestAction1 extends Action { public function __construct($cParent, $sMethod) { parent::__construct($cParent, $sMethod); } } class TestAction2 extends Action { private $maybeNeedLater; public function __construct($cParent, $sMethod) { $this->maybeNeedLater = $cParent; parent::__construct($this, $sMethod); // pass $this instead } } $testThis = new B(); $testThis->process(); $serialized = serialize($testThis); $testThis = unserialize($serialized); The Action class TestAction2 will have a null $parent field in the $actions field of $testThis
__sleep()and__wakeup()magic methods to save and recreate the links between objects.Serializableinterface in place of the magic methods; and this was all working before I had implemented theSerializableinterface on the class A that is holding reference to theActioninstances.