0
0
PHPprogramming~10 mins

__serialize and __unserialize in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - __serialize and __unserialize
Object created
Call __serialize()
Return array of properties
Serialize array
Store or transmit serialized data
Call __unserialize(array)
Restore object properties
Object ready to use
When an object is serialized, PHP calls __serialize() to get data. When unserialized, __unserialize() restores the object from that data.
Execution Sample
PHP
<?php
class User {
  private string $name;
  private int $age;

  public function __construct(string $name, int $age) {
    $this->name = $name; $this->age = $age;
  }

  public function __serialize(): array {
    return ['name' => $this->name, 'age' => $this->age];
  }

  public function __unserialize(array $data): void {
    $this->name = $data['name'];
    $this->age = $data['age'];
  }
}

$user = new User("Anna", 30);
$serialized = serialize($user);
$unserialized = unserialize($serialized);
This code shows how __serialize and __unserialize convert an object to array and back during serialization.
Execution Table
StepActionInputOutputNotes
1Create User objectname='Anna', age=30User object with properties setObject initialized
2Call serialize()User objectCalls __serialize()Returns ['name'=>'Anna', 'age'=>30]
3serialize() converts array to string['name'=>'Anna', 'age'=>30]Serialized stringString format for storage/transmission
4Call unserialize()Serialized stringCalls __unserialize() with arrayArray extracted from string
5__unserialize() sets properties['name'=>'Anna', 'age'=>30]User object restoredProperties restored from array
6Use unserialized objectUser objectAccess properties name='Anna', age=30Object ready to use
7EndN/AN/ASerialization cycle complete
💡 Serialization ends after __unserialize restores object properties
Variable Tracker
VariableStartAfter serializeAfter unserializeFinal
$userUser(name='Anna', age=30)User(name='Anna', age=30)User(name='Anna', age=30)User(name='Anna', age=30)
$serializedN/ASerialized stringSerialized stringSerialized string
$unserializedN/AN/AUser(name='Anna', age=30)User(name='Anna', age=30)
Key Moments - 2 Insights
Why does PHP call __serialize() instead of directly serializing properties?
PHP calls __serialize() to let the object control which data to save, shown in step 2 of execution_table where __serialize returns a custom array.
What happens if __unserialize() does not restore all properties?
The object will be incomplete or broken after unserialization, as seen in step 5 where __unserialize sets properties; missing this step means missing data.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does __serialize() return at step 2?
AThe original object
BAn array of object properties
CA serialized string
DNull
💡 Hint
See step 2 in execution_table where __serialize returns an array
At which step does __unserialize() restore the object properties?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Step 5 in execution_table shows __unserialize setting properties
If __serialize() returned an empty array, what would happen after unserialize?
AObject properties would be empty or default
BObject would have original properties
CSerialization would fail
DObject would be null
💡 Hint
Refer to variable_tracker and key_moments about property restoration
Concept Snapshot
__serialize and __unserialize methods let PHP objects control serialization.
__serialize() returns an array of data to save.
__unserialize(array) restores object from that data.
Called automatically by serialize() and unserialize().
Use to customize what and how object data is saved.
Full Transcript
This visual trace shows how PHP uses __serialize and __unserialize methods during object serialization. First, an object is created with properties. When serialize() is called, PHP calls __serialize() to get an array of properties to save. This array is converted to a string for storage or transmission. Later, unserialize() converts the string back to an array and calls __unserialize() to restore the object's properties. The object is then ready to use with its original data. Key points include that __serialize controls what data is saved, and __unserialize must restore all needed properties to avoid broken objects.