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
get_class_vars + array_diff
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
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
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
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"
}
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
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
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"
}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
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
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
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"
}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
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
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
Hi
2009/6/1 Nathan Rixham nrixham@gmail.com:
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"
I've recently fixed the manual about get_class_vars, since PHP 5.0.5
get_class_vars, depending on the scope will return the properties
thats accessable from this scope, so calling it inside a method will
expose both its public/protected/private properties.
need private and inherited private
--
regrads,
Kalle Sommer Nielsen
kalle@php.net