I've worked with PHP for a few years now, but up until now never had a need to deal with serialisation explicitly, only using the $_SESSION
. Now I have a project that requires me to manually implement serialisation mechanism for certain data - and I realise that the issue is applicable to $_SESSION
as well.
I have a class that contains a number of properties. Most of these properties are small (as in memory consumption): numbers, relatively short strings, etc. However the class also contains some properties, which may contain HUGE arrays (e.g. an entire dump of a database table: 100,000 rows with 100 fields each). As it happens, this is one of the classes that needs to be serialised/deserialised - and, luckly, the properties containing large arrays don't need to be serialised, as they are essentially temporary pieces of work and are rebuilt anyway as necessary.
In such circumstances in Java, I would simply declare the property as transient
- and it would be omitted from serialisaion. Unfortunately, PHP doesn't support such qualifiers.
One way to deal with is it to have something like this:
class A implements Serializable
{
private $var_small = 1234;
private $var_big = array( ... ); //huge array, of course, not init in this way
public function serialize()
{
$vars = get_object_vars($this);
unset($vars['var_big']);
return serialize($vars);
}
public function unserialize($data)
{
$vars = unserialize($data);
foreach ($vars as $var => $value) {
$this->$var = $value;
}
}
}
However this is rather cumbersome, as I would need to update serialize
method every time I add another transient property. Also, once the inheritance comes into play, this becomes even more complicated - to deal with, as transient properties may be in both subclass and the parent. I know, it's still doable, however I would prefer to delegate as much as possible to the language rather than reinvent the wheel.
So, what's the best way to deal with transient properties? Or am I missing something and PHP supports this out of the box?
Transient in Java is used to mark the member variable not to be serialized when it is persisted to streams of bytes. This keyword plays an important role to meet security constraints in Java. It ignores the original value of a variable and saves the default value of that variable data type.
transient Fields: Variables may be marked transient to indicate that they are not part of the persistent state of an object. For example, you may have fields that are derived from other fields, and should only be done so programmatically, rather than having the state be persisted via serialization.
transient variable in Java is a variable whose value is not serialized during Serialization and which is initialized by its default value during de-serialization, for example for object transient variable it would be null.
transient is a variables modifier used in serialization. At the time of serialization, if we don't want to save value of a particular variable in a file, then we use transient keyword. When JVM comes across transient keyword, it ignores original value of the variable and save default value of that variable data type.
Php provides __sleep magic method which allows you to choose what attributes are to be serialized.
EDIT I've tested how does __sleep()
work when inheritance is in the game:
<?php
class A {
private $a = 'String a';
private $b = 'String b';
public function __sleep() {
echo "Sleep A\n";
return array( 'a');
}
}
class B extends A {
private $c = 'String c';
private $d = 'String d';
public function __sleep() {
echo "Sleep B\n";
return array( 'c');
}
}
class C extends A {
private $e = 'String e';
private $f = 'String f';
public function __sleep() {
echo "Sleep C\n";
return array_merge( parent::__sleep(), array( 'e'));
}
}
$a = new A();
$b = new B();
$c = new C();
echo serialize( $a) ."\n"; // Result: O:1:"A":1:{s:4:"Aa";s:8:"String a";}
// called "Sleep A" (correct)
echo serialize( $b) ."\n"; // Result: O:1:"B":1:{s:4:"Bc";s:8:"String c";}
// called just "Sleep B" (incorrect)
echo serialize( $c) ."\n"; // Caused: PHP Notice: serialize(): "a" returned as member variable from __sleep() but does not exist ...
// When you declare `private $a` as `protected $a` that class C returns:
// O:1:"C":2:{s:4:"*a";s:8:"String a";s:4:"Ce";s:8:"String e";}
// which is correct and called are both: "Sleep C" and "Sleep A"
So it seems that you can serialize parent data only if it's declared as protected :-/
EDIT 2 I've tried it with Serializable
interface with following code:
<?php
class A implements Serializable {
private $a = '';
private $b = '';
// Just initialize strings outside default values
public function __construct(){
$this->a = 'String a';
$this->b = 'String b';
}
public function serialize() {
return serialize( array( 'a' => $this->a));
}
public function unserialize( $data){
$array = unserialize( $data);
$this->a = $array['a'];
}
}
class B extends A {
private $c = '';
private $d = '';
// Just initialize strings outside default values
public function __construct(){
$this->c = 'String c';
$this->d = 'String d';
parent::__construct();
}
public function serialize() {
return serialize( array( 'c' => $this->c, '__parent' => parent::serialize()));
}
public function unserialize( $data){
$array = unserialize( $data);
$this->c = $array['c'];
parent::unserialize( $array['__parent']);
}
}
$a = new A();
$b = new B();
echo serialize( $a) ."\n";
echo serialize( $b) ."\n";
$a = unserialize( serialize( $a)); // C:1:"A":29:{a:1:{s:1:"a";s:8:"String a";}}
$b = unserialize( serialize( $b)); // C:1:"B":81:{a:2:{s:1:"c";s:8:"String c";s:8:"__parent";s:29:"a:1:{s:1:"a";s:8:"String a";}";}}
print_r( $a);
print_r( $b);
/** Results:
A Object
(
[a:A:private] => String a
[b:A:private] =>
)
B Object
(
[c:B:private] => String c
[d:B:private] =>
[a:A:private] => String a
[b:A:private] =>
)
*/
So to sum up: you can serialize classes via __sleep()
only if they don't have private members in super class (which need to be serialized as well). You can serialize complex object via implementing Serializable
interface, but it brings you some programming overhead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With