note 76643 deleted from language.oop5.magic by cmb

From: Date: Sun, 10 Nov 2019 12:40:01 +0000
Subject: note 76643 deleted from language.oop5.magic by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212989@lists.php.net to get a copy of this message
Note Submitter: amir_abiri at ipcmedia dot com ---- Another small thing that is important to note about __sleep() and privte member variables: <?php class A { private $a; public function __construct() { $this->a = 1; } } class B extends A { protected $b; public function __construct() { parent::__construct(); $this->b = 2; } function __sleep() { return array('a', 'b'); } } serialize(new B); ?> result: Notice: serialize(): "a" returned as member variable from __sleep() but does not exist in ... To summerize: in a given class hierarchy in which parent classes contain private member variables, those variables are serialized when __sleep() is not defined. However, once __sleep() is defined, there is no way to make those private member variables serialized as well. From that point on, serialization is performed from the visibility scope of the subclass. It is particularly important to note this little quirk when designing base classes that their derivables may be serialized, or when subclassing an external library class.

« previous php.notes (#212989) next »