Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114961 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 28297 invoked from network); 18 Jun 2021 16:03:21 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Jun 2021 16:03:21 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8A3281804C8 for ; Fri, 18 Jun 2021 09:20:39 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: * X-Spam-Status: No, score=1.4 required=5.0 tests=BAYES_05,BODY_8BITS, KHOP_HELO_FCRDNS,SPF_HELO_NONE,SPF_NONE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from processus.org (ns366368.ip-94-23-14.eu [94.23.14.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 18 Jun 2021 09:20:38 -0700 (PDT) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by processus.org (Postfix) with ESMTPA id E30695101324; Fri, 18 Jun 2021 16:20:36 +0000 (UTC) To: Nikita Popov Cc: php internals References: <1cee07dc-83aa-a9bd-7219-a7b339c701e1@processus.org> Message-ID: Date: Fri, 18 Jun 2021 18:20:36 +0200 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Authentication-Results: processus.org; auth=pass smtp.auth=pierre-php@processus.org smtp.mailfrom=pierre-php@processus.org X-Spamd-Bar: / Subject: Re: [PHP-DEV] Simple support question, about object equality From: pierre-php@processus.org (Pierre) Le 18/06/2021 à 18:08, Nikita Popov a écrit : > == comparison on objects is a == comparison on the properties. Whether the > properties are private doesn't (and shouldn't) matter. > > Regards, > Nikita Thanks a lot for your answer. This raises more question in my mind: I need to rephrase here. My use case is the following, we have a custom identifier class in a project which does look like the code above: uuid;     }     public function equals($other): bool     {         return $other instanceof UuidIdentifier && $this->uuid->equals($other->getInternalUuid());     } } ?> Actually, use case is not exactly the same, but somehow looks like this. Using the object equality, using `==` and `!=` some code of a colleague did work gracefully in unit tests, and in some other integration tests, but coverage did not reflect the real possible scenarios. Fact is:  - UuidInterface from ramsey/uuid is an opaque structure, that gives a contract but no guarantees about the internal reprensentation,  - it could be a LazyUuid, and Uuid4 or any other implementation, which all yield the same value, but not the same instance class,  - any UuidInterface instance could have been decorated and not be the same class name,  - all unit tests did build its objects using the same method, guaranting the same deep-equality, but the final application doesn't guarantee that all objects will be created the same way. I spotted this during a review, and told him "that's weird, it should not work" and we had an infernal discussion until he found the https://www.php.net/manual/en/language.oop5.object-comparison.php page, but I finally dissuaded him to proceed this way and use the ->equals() method instead, which is an explicit and clear contract. This implicit deep object equality in PHP could have caused some serious damage later in production, if, for any reason, we would have decorated some UuidInterface instances, or just instanciated those in many ways. Wouldn't it be a good reason to implement an Equatable/Comparable interface in PHP, or a magic __compareTo() or __equals() method to allow us, in such scenario, to write a robust and predictible == and != behavior ? This already existing RFC https://wiki.php.net/rfc/comparator_interface might not be the best one, but it's one to start with. Regards, -- Pierre