I've a persistent library that should store objects in a database. So var_export()
should store the type, pk, and db-version only. At the reverse it should recreated the object from the database using the key. In other words:
class Person
{
use PersistTrait; // $id, $store, __get, __set
protected string $name;
protected string $website;
public function __construct(string $id)
{
// connect to db
// retrieve object content by $id
}
public static function __set_state($array_with_only_id): Person
{
// check version
return new self( $array_with_only_id['id'] ); // exception when not found etc.
}
}
$person = new \Person();
$person->name = 'ElePHPant ElePHPantsdotter';
$person->website = 'https://php.net/elephpant.php';
$person->store();
$c = var_export($person);
// $c should only contain "
Person::__set_state(
[
'id'=>'pk-value-of-person',
'version'=>'db-version'
]
);
Is this possible?
Serializable::serialize()
andSerializable::unserialize()
would be different use cases, wouldn't they? I'm not sure why__serialize()
/__unserialize()
exist. But yes,var_export()
probably is concepted for a specific use case I'm not aiming here.