note 48849 added to language.oop5.magic

From: Date: Sun, 09 Jan 2005 08:09:05 +0000
Subject: note 48849 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-82973@lists.php.net to get a copy of this message
If you are attempting to write an abstract/base class which automates the __sleep process in PHP5 you will run into some trouble if the subclasses which are being serialized have private/protected variables you need to be serialized. The reason is, even though get_class($this) within the base class will return the subclass -- get_class_vars(get_class($this)) will *not* return the subclass' protected/private variables. Which makes sense -- using OO principles. However, when automating __sleep it becomes necissary to have access to the private/protected subclass variables because their names have to be returned by __sleep. So here is the work around: <?php public function __sleep() { ... code ... $sleepVars = array_keys((array)$this); return $sleepVars; } ?> Even though array_keys includes more information about the variable names than just the variable names -- it still seems to work appropriately. ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48849 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48849&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48849&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48849&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48849&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48849&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48849&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48849&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#82973) next »