Re: ARRAY_UNIQUE_IDENTICAL option

From: Date: Fri, 04 Nov 2022 14:55:06 +0000
Subject: Re: ARRAY_UNIQUE_IDENTICAL option
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-118965@lists.php.net to get a copy of this message
On 04.11.2022 at 10:05, Rowan Tommins wrote: > On 3 November 2022 18:53:40 GMT, someniatko <someniatko@gmail.com> wrote: > >> You will have to memorize yet another PHP quirk, or be able to build a >> logical chain: >> - enums are non-comparable by default >> - enums have no default string value (if not baked by a string) >> - array_unique internally sorts an array >> - default flag for array_unique compares the string representations of its items >> - thus it won't work for enums in a general case > > Actually, I think this is already the case for "normal" objects - I had no idea that > array_unique used a string cast to compare objects, so am very surprised that it will not consider > objects of completely different classes unique, if they happen to have the same string value: > https://3v4l.org/UGCvB > > Making backed enums work with their backing value would be equally confusing to me - > Day::MONDAY and Month::JANUARY might both be backed by a 1, but they are certainly distinct values. > I'd much rather get an error that made me check the manual and find a flag than have one of > them silently discarded. I agree. In my opinion, we should *consider* to *always* raise a warning on attempts to compare incomparable values. As it is now, silently returning false looks like a footgun to me. -- Christoph M. Becker

« previous php.internals (#118965) next »