2017 © Pedro Peláez
 

library magicalserializer

Serialize Volatile, Threaded or any other internal PHP class!

image

danog/magicalserializer

Serialize Volatile, Threaded or any other internal PHP class!

  • Friday, April 27, 2018
  • by danog
  • Repository
  • 3 Watchers
  • 4 Stars
  • 14,414 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 20 % Grown

The README.md

MagicalSerializer

Licensed under AGLPV3, created by Daniil Gentili (https://daniil.it)., (*1)

This library allows you to serialize classes that extend pthreads's Volatile or Threaded classes, or any other internal class with a custom serializer., (*2)

It also provides reverse and forward compatibility between old serializations, where your objects do not yet extend Threaded/Volatile, and new serializations, where they do extend them., (*3)

Install it using composer, the package name is danog/magicalserializer., (*4)

Usage of this library is extremely simple, here are a few examples (see a.php and b.php for more):, (*5)

<?php

class a
{
    public function __construct() {
        var_dump("Constructed!");
        $this->a = 'pony';
    }
    public function __wakeup() {
        var_dump($this->a);
    }
}
$a = new a;
file_put_contents('test', serialize($a));

As you can see, here a does not extend any class, and we are serializing an instance of it to the file test. This example also prints Constructed!, since the constructor function, __magic_construct, is called., (*6)

<?php

class a extends \Volatile
{
    use \danog\Serializable;
    public function __magic_construct() {
        var_dump("Constructed!");
        $this->a = 'pony';
    }
    public function __wakeup() {
        var_dump($this->a);
    }
}
$a = \danog\Serialization::unserialize(file_get_contents('test'));
var_dump($a);
$othera = new a;

Here we must simply include the trait \danog\Serializable to make the class serializable.
This will deserialize correctly the contents of test, recreating the instance of a, with the only difference that now it will be thread safe.
Of course, the wakeup function will be called, so pony will be printed.
You may have noticed that the construct function is now called __magic_construct instead of __construct: this is a required change, or else this library will not work.
Of course, when a will be instantiated, the constructor function will be called anyway (and you'll see the message Constructed! pop up)., (*7)

If you try to serialize (using \danog\Serialization::serialize) either $a or $othera and deserialize it from the first script (where a does not extend any other class), you will find that it will be deserialized correctly., (*8)

The Versions

27/04 2018

dev-master

9999999-dev https://daniil.it/MagicalSerializer

Serialize Volatile, Threaded or any other internal PHP class!

  Sources   Download

AGPLv3 AGPL-3.0-only

by Daniil Gentili

serialize pthreads threading serializable volatile

20/02 2018

1.0

1.0.0.0 https://daniil.it/MagicalSerializer

Serialize Volatile, Threaded or any other internal PHP class!

  Sources   Download

AGPL-3.0-only

by Daniil Gentili

serialize pthreads threading serializable volatile