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 On Sun, May 31, 2009 at 11:01 PM, Nathan Rixham <nrix...@gmail.com> wrote: > matt.. that's public properties on a single class - see the usecase > > use case: > <?php > class base > { > private $notToBeSerialized; > > public function __sleep() > { > // TODO code return instance properties excluding $notToBeSerialized > } > > } > > class foo extends base > { > private $bar; > } > > class poo extends foo > { > private $baz; > } > > $p = new poo; > echo serialize($p); > > > Matt Wilson wrote: >> >> mwil...@mattw-mac:~$ php -r 'class a { public $a, $b, $c, $d; public >> function __sleep() { return array_diff( array_keys( (array) $this), >> array("b","c")); } } $a = new a; var_dump($a->__sleep());' >> array(2) { >> [0]=> >> string(1) "a" >> [3]=> >> string(1) "d" >> } >> >> On May 31, 2009, at 8:46 PM, Nathan Rixham wrote: >> >>> Matt Wilson wrote: >>>> >>>> get_class_vars + array_diff >>> >>> cheers but nope; as the manual says >>> "Returns an associative array of default public properties of the class" >>> >>> need private and inherited private >>> >>>> On May 31, 2009, at 8:04 PM, Nathan Rixham wrote: >>>>> >>>>> Hi All, >>>>> >>>>> hoping somebody can help me here.. >>>>> >>>>> I need to implement an inverted __sleep method, by which I mean to >>>>> specify what variables should not be included. >>>>> >>>>> use case: >>>>> <?php >>>>> class base >>>>> { >>>>> private $notToBeSerialized; >>>>> >>>>> public function __sleep() >>>>> { >>>>> // TODO code return instance properties excluding $notToBeSerialized >>>>> } >>>>> >>>>> } >>>>> >>>>> class foo extends base >>>>> { >>>>> private $bar; >>>>> } >>>>> >>>>> class poo extends foo >>>>> { >>>>> private $baz; >>>>> } >>>>> >>>>> $p = new poo; >>>>> echo serialize($p); >>>>> >>>>> ---- >>>>> >>>>> I've tried using get_object_vars($this), and ReflectionObject($this) >>>>> both of which don't include all the properties of the instance. >>>>> >>>>> the only way I can see to get all the properties of the instance is to >>>>> serialize($this) then parse the string, but that's just wrong and what >>>>> would >>>>> sleep return on the first call to serialize. >>>>> >>>>> any help greatly appreciated. >>>>> >>>>> regards, >>>>> >>>>> nathan >>>>> >>>>> -- >>>>> PHP Internals - PHP Runtime Development Mailing List >>>>> To unsubscribe, visit: http://www.php.net/unsub.php >>>>> >>> >> > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php