Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danog/magicalserializer
Serialize internal PHP objects!
https://github.com/danog/magicalserializer
php pthreads serializable-objects serialization threading volatile
Last synced: 17 days ago
JSON representation
Serialize internal PHP objects!
- Host: GitHub
- URL: https://github.com/danog/magicalserializer
- Owner: danog
- License: agpl-3.0
- Created: 2017-04-30T16:39:40.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-27T16:42:39.000Z (over 6 years ago)
- Last Synced: 2024-10-12T15:58:49.880Z (about 1 month ago)
- Topics: php, pthreads, serializable-objects, serialization, threading, volatile
- Language: PHP
- Homepage:
- Size: 34.2 KB
- Stars: 9
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MagicalSerializer
Licensed under AGLPV3, created by Daniil Gentili (https://daniil.it).
This library allows you to serialize classes that extend pthreads's Volatile or Threaded classes, or any other internal class with a custom serializer.
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.
Install it using composer, the package name is `danog/magicalserializer`.
Usage of this library is extremely simple, here are a few examples (see a.php and b.php for more):
```
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.```
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).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.