class Object { function __sleep() { return array_keys( (array)$this ); } } class BaseClass extends Object { private $foo; private $dbh; // some resource, like a database handler function __sleep() { unset($this->dbh); return parent::__sleep(); } } class SubClass extends BaseClass { private $bar; function __sleep() { return parent::__sleep(); } }
The point is, the subclasses do any local cleanup then delegate the actual __sleep request to a parent class. With a common parent class Object that all other classes extend, we can keep the real serialize code in one place but still allow children to change their state before sleeping.