PHP: Serializing an unserializing the simple way

Additionally, there are some caveats when using the default serialization:The string is difficult to understand at first glance, since PHP uses a weird encoding to keep the Class shape.

You have to take into account that the properties of the class instance are also serializable or not, and have a work-around to restore them on unserialization.

As you can see, they seems counterproductive and confusing, but since PHP 5.

1 there is no more need to take them into account since there is a much, much better way to handle serialization.

Welcome Serializable!PHP has a handy interface called Serializable.

This contains two methods you must set, called serialize() and unserialize(), and they replace the old calls without breaking backwards compatibility.

They’re pretty much self-explanatory, but in any case, here the gist:serialize() is called when the class is being serialized.

Its purpose is to pass an string representation of the Class to PHP.

For example, we can just return a JSON-encoded list of attributes.

The class name is appended automatically with the returned string— without the latter, PHP won’t know to which class to unserialize later.

unserialize() is called when the string is being unserialized to the corresponding Class.

PHP will check the class name in the serialized string, pass the string to this method, and set whatever the class instance needs.

The unserialize() method is tricky to understand at first glance, but falter not.

As the documentation says, this method acts like a “constructor”, meaning, the class will be instanced bypassing the __construct() method.

If you need some dependencies set in the _contruct(), the unserialize method will have to look for them manually, or (preferably) set them when the serialization is done:$car = unserialize(FileHandler::get('class.

txt'));$car->setWheels(new Wheels());$car->setPaintJob(new PaintJob('red'));For setting the dependencies, a good way is to use setters on the unserialized class instance using the help of the ReflectionClass’s getConstructor() and the ReflectionMethod’s getParameters(), if you need something more advanced and modular.

But we’re getting ahead.

Let’s serialize something.

Serializing and unserializing a CarYou wouldn’t download a Car?.Not, still, but we can save it in a file.

A simple glance on this “Car” class and you will note that it needs an Engine, we offer a PHP 7.

4-style property to set the color, and it has serialization methods already set.

When it serializes, it will do it as JSON.

In that string, we will put the Engine class name, if the Engine it’s on, and the color.

When it unserializes, we will decode the JSON string.

We will create a new Engine from the class name given, put the color, and start the engine if it was started.

Let’s check it in action.

And voilà.

No need to do weird sh*t in your code to save a Class instance.

Hell, you could even save them into a USB stick and unserialize them in another computer.

Technology!Word of advice: you cannot serialize Closures, mainly because these have a context outside the Class.

It’s like receiving a bill to pay but not knowing to whom.

But… if you need that functionality, the SuperClosure package may be for you.