Re: need inverted __sleep?
| From: | Nathan Rixham | Date: | Mon, 01 Jun 2009 02:01:30 +0000 |
| Subject: | Re: need inverted __sleep? | ||
| References: | 1 2 3 4 | Groups: | php.internals php.internals |
| Request: | Send a blank email to internals+get-44111@lists.php.net to get a copy of this message | ||
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:
mwilson@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_diffcheers but nope; as the manual says "Returns an associative array of default public properties of the class" need private and inherited privateOn 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