Hello, internals,

Currently, this is an Error:

$allowedRoles = [Role::Admin];
$hasRoles = [Role::User, Role::Admin];

if(!empty(array_intersect($allowedRoles, $hasRoles))) doSomething();

This is an error because enums cannot be cast to a string (and trying
to make them stringable is not allowed) and many array functions
expect something stringable. To work around this limitation, the above
example must be rewritten like this:

array_uintersect($allowedRles, $hasRoles, fn($l, $r) => $l === $r ? 0 : 1));

I'd like to propose an RFC that allows array functions to accept
stringable values and enum values. Any other values (like general
objects) would be out of scope.

I foresee someone bringing up how the natural order of an enum would
be determined. The order will be determined via the same order as
Enum::cases() outputs, independent of their backed value (e.g.,
lexicographical order as defined in the code), thus the order of

enum Alpha : int { case B = 1; case A = 0; }

would be [ Alpha::B, Alpha::A ]. This is what happens when calling
sort([Alpha::A, Alpha::B]) currently, so I hope this won't be an
issue.

Some example functions that would be impacted by this RFC (I still
haven't dug into all the array functions yet):

- array_intersect
- array_diff
- array_unique? (works with SORT_REGULAR, so this might need
discussion/exemption)

If this has been proposed before, I apologize, as I did search for
something like this before proposing it.

Robert Landers
Software Engineer
Utrecht NL

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to