Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112473 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 33350 invoked from network); 8 Dec 2020 22:34:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Dec 2020 22:34:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1DFF11804E0 for ; Tue, 8 Dec 2020 14:04:09 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=BAYES_05,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail01.nexxes.net (mail01.nexxes.net [144.76.185.252]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 8 Dec 2020 14:04:07 -0800 (PST) Received: from [IPv6:2001:4dd4:eb58:0:9d8f:b298:d7db:45c2] (2001-4dd4-eb58-0-9d8f-b298-d7db-45c2.ipv6dyn.netcologne.de [IPv6:2001:4dd4:eb58:0:9d8f:b298:d7db:45c2]) (Authenticated sender: db220660-p0g-1) by mail01.nexxes.net (Postfix) with ESMTPSA id 37EFD86063B for ; Tue, 8 Dec 2020 23:04:06 +0100 (CET) To: internals@lists.php.net References: <79044037-4cd4-4065-a3c1-39a4deea3e09@www.fastmail.com> <763bd813-88f4-4981-820d-a1e50e4c2e1e@www.fastmail.com> Message-ID: <98d8351e-8e7d-dc09-c466-ad986ebe990a@dennis.birkholz.biz> Date: Tue, 8 Dec 2020 23:04:04 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 MIME-Version: 1.0 In-Reply-To: <763bd813-88f4-4981-820d-a1e50e4c2e1e@www.fastmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Enumerations From: php@dennis.birkholz.biz (Dennis Birkholz) Am 08.12.20 um 18:40 schrieb Larry Garfield: > For serialization, we'll introduce a new serialization marker, enum, which will make it feasible too round-trip an enum while maintaining singleton-ness. More specifically, the deserialize routine would essentially become $type::from($value), where $type and $value are pulled from the serialized version. I wonder whether this mechanism could be generalized to a serialization mechanism that would allow e.g. Database Repositories to pull entities from a database upon unserialization. My example would something like this (simplified): interface IdentifierBasedSerializable { static function serializeToIdentifier(self $object) : string; static function unserializeFromIdentifier(string $identifier) : self } class ExternallyStoredEntity implements IdentifierBasedSerializable { static function serializeToIdentifier(self $object) : string { return $object->id; } static function unserializeFromIdentifier(string $identifier): self{ if (!\array_key_exists($identifier, self::$objectCache)) { self::$objectCache[$identifier] = new self($identifier); } return self::$objectCache[$identifier]; } private static $objectCache = []; public string $id; public function __construct(string $id) { $this->id = $id; } } Until now it is not possible to unserialize an object to the canonical instance created earlier in the program execution. Serialization and subsequent unserialization breaks object identity ($a->id === $b->id <=> $a === $b) that is often desired when working with objects that represent external entities. The list of allowed enum values is in this regard a very simple database of identifiers. I know that passing a complex serialization string to a static unserialization factory method has the serious problem that circular references can not be handled. But using only an identifier as the sole contents of the serialization string avoids this problem entirely. Greets Dennis