This is my first SO question, although I was essentially looking; I apologize if this has already been affected.
The question / issue is related to PHP serialize () functionality. I use serialization to store objects in a database. For instance:
class Something {
public $text = "Hello World";
}
class First {
var $MySomething;
public function __construct() {
$this->MySomething = new Something();
}
}
$first_obj = new First();
$string_to_store = serialize($first_obj);
echo $string_to_store
Now, later in the life of the project, I want to change my class: first, to have a new property: $ SomethingElse, which will also correspond to the Something object.
, / , , , (SomethingElse) - __wakeup() . . ? ( ).
, , unserialize, , Something :
class Something {
public $text = "Hello World";
public $new_text = "I would be in the unserialized old version.";
}
...
$obj = unserialize('O:5:"First":1:{s:11:"MySomething";O:9:"Something":1:{s:4:"text";s:11:"Hello World";}}');
print_r($obj);
, ( __wakeup()?).
, . , - , , ( init) __wakeup() , . .
.
: , , init():
class Something {
public $text = "Hello World2";
public $new_text = "I would be in the unserialized old version.2";
}
class First {
var $MySomething;
var $SomethingElse;
public function __construct() {
$this->init();
}
public function __wakeup() {
$this->init();
}
private function init() {
if (!isset($this->MySomething)) {
$this->MySomething = new Something();
}
if (!isset($this->SomethingElse)) {
$this->SomethingElse = new Something();
}
}
}
$new_obj = unserialize('O:5:"First":1:{s:11:"MySomething";O:9:"Something":1:{s:4:"text";s:11:"Hello World";}}');
print_r($new_obj);
, , , . , , .