Re: need inverted __sleep?

From: Date: Mon, 01 Jun 2009 11:34:52 +0000
Subject: Re: need inverted __sleep?
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-44115@lists.php.net to get a copy of this message
Jonathan Tapicer wrote:
Hi, Matt's approach works also for your usecase, casting to array returns all the properties of the class and base classes, but it has some problems: for private properties the class name is added before the property name, and for protected properties * is added, both things added are between null characters, so you can remove the added string with some regexp. Another approach that will work is using ReflectionClass and the method getProperties, it will return all the properties (even private), but only of the class itself, it won't return the properties declared in base classes, you have the get the base class (get_parent_class) and recursively do the same thing for all the base classes. Regards, Jonathan
doh, thanks Jonathan and thanks Matt - completely missed that (array) even though I'd been using it in a different attempt using Serializable small note is that you do not need to remove the class names or suchlike, this works fine (and I assume is faster than recursive reflection) public function __sleep() { $exclude = chr(0) . __CLASS__ . chr(0) . 'notToBeSerialized'; return array_diff(array_keys((array)$this) , array($exclude) ); } again thanks, and apologies Matt! Regards, Nathan

« previous php.internals (#44115) next »