note 55783 modified in language.oop5.magic by mazzanet

From: Date: Sat, 13 Aug 2005 08:46:30 +0000
Subject: note 55783 modified in language.oop5.magic by mazzanet
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-93537@lists.php.net to get a copy of this message
In reply to krisj1010 at gmail.com below: __sleep() handles protected/private properties very well. You should never rely on get_class_vars() to retrieve property names since this function only returns the public properties. Use the Reflection API instead for that purpose. Better yet, if you know which ones you want to save it is always faster to specify the return array manually. --was-- Many of us know but many don't that, in PHP5 (and 4) saving an object in the session will *not* automatically call the magic method __sleep(), even though the actual object *is* serialized when session data is written (you can check the raw session contents). Same goes for __wakeup(). What I mean is, if you save your object in the session like this: <?php $_SESSION['myOBJ'] = $myOBJ; ?> ... then $myOBJ->__sleep() will *not* be called. To make sure the __sleep() method is called you have to manually call serialize() over the object when saving it, like this: <?php $_SESSION['myOBJ'] = serialize($myOBJ); ?> Of course, you will have to manually unserialize() it as well afterwards or $_SESSION['myOBJ'] will remain a string (duh :)) One other note, in reply to krisj1010 at gmail.com below: __sleep() handles protected/private properties very well. You should never rely on get_class_vars() to retrieve property names since this function only returns the public properties. Use the Reflection API instead for that purpose. Better yet, if you know which ones you want to save it is always faster to specify the return array manually. have a good __sleep() http://php.net/manual/en/language.oop5.magic.php

« previous php.notes (#93537) next »