...

/

Introducing the Serializable Interface

Introducing the Serializable Interface

Learn about the issues associated with the Serializable interface and how to handle them using new magic methods introduced in PHP 8.

In order to facilitate the serialization of objects, the Serializable interface was added to the language beginning with PHP 5.1. The idea behind this interface was to provide a way of identifying objects that had the ability to serialize themselves. In addition, the methods specified by this interface were designed to provide some degree of control over object serialization.

As long as a class implements this interface, developers are assured that two methods are defined: serialize() and unserialize(). Here is the interface definition:

interface Serializable {
public serialize () : string|null
public unserialize (string $serialized) : void
}

Any class that implements this interface has its custom serialize() and unserialize() methods automatically invoked during native serialization or unserialization. To illustrate this technique, consider the following example:

Press + to interact
<?php
// Create a class "A" that implements the "Serializable" interface.
class A implements Serializable {
// Define private properties "a", "b", and "u" with default values.
private $a = 'A';
private $b = 'B';
private $u = NULL;
// Implement the "serialize()" method required by the "Serializable" interface.
// This method is automatically called when an object of this class is being serialized.
public function serialize() {
// Before serialization, create a new "DateTime" object and assign it to the "u" property.
// This is an example of adding additional data to the serialized object.
$this->u = new DateTime();
// Serialize the object's properties using "get_object_vars()".
return serialize(get_object_vars($this));
}
// Implement the "unserialize()" method required by the "Serializable" interface.
// This method is automatically called when an object of this class is being unserialized.
public function unserialize($payload) {
// Unserialize the payload to get the serialized properties as an array.
$vars = unserialize($payload);
// Iterate through the array and assign each property value back to the object.
foreach ($vars as $key => $val) {
$this->$key = $val;
}
}
}
// Create an instance of the "A" class and assign it to the variable $a1.
$a1 = new A();
// Output the contents of the $a1 object before serialization using "var_dump()".
var_dump($a1);
// Serialize the $a1 object into a string and assign it to the variable $str.
$str = serialize($a1);
// Unserialize the $str string back into an object and assign it to the variable $a2.
$a2 = unserialize($str);
// Output the contents of the $a2 object after unserialization using "var_dump()".
var_dump($a2);
?>

Let’s get into the code.

  • Lines 3–7: First, we define a class that implements the Serializable interface. The class defines three properties: two of type string, the other representing date and time.

  • Lines 11–30: We then define a custom serialize() method that initializes the date and time before serializing the object’s properties. The unserialize() method restores values to all ...