Skip to content

EnumSerializableTrait #52

@marc-mabe

Description

@marc-mabe

Implementing a trait than can simply be used to make an enumeration serializable.

As long as it's impossible in PHP to make a singleton serializable this functionality should be used carefully and needs to be implemented itself. This trait will help to activate it if really required.

Usage example:

use MabeEnum\Enum;
use MabeEnum\EnumSerializableTrait;
use Serializable;

class CardinalDirection extends Enum implements Serializable
{
    use EnumSerializableTrait;

    const NORTH = 'n';
    const EAST = 'e';
    const WEST = 'w';
    const SOUTH = 's';
}

$north1 = CardinalDirection::NORTH();
$north2 = unserialize(serialize($north1));
var_dump($north1 === $north2); // FALSE <- this is the wrong behavior you could run into

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions