mgdigital / deconstructor
Installs: 119
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/mgdigital/deconstructor
Requires
- php: >=7.0
This package is not auto-updated.
Last update: 2018-02-16 12:34:27 UTC
README
What?
Classes can have constructors, so why not deconstructors?
Why?
Allows an object to describe how it was instantiated, allowing identical copies to be created from its constructor arguments.
How?
<?php
class ValueObject implements MGDigital\Deconstructor\DeconstructorInterface
{
private $property;
public function __construct($property)
{
$this->property = $property;
}
public function deconstruct(): array
{
return [$this->property];
}
public function getProperty()
{
return $this->property;
}
}
$instance = new ValueObject('value');
$deconstruction = MGDigital\Deconstructor\Deconstruction::deconstruct($instance);
$copy = $deconstruction->construct();
echo $copy->getProperty(); // value