Relating to the existing thread in my introduction:
Re: [PHP-DEV] Introduction - SuitespaceRnD
I am requesting RFC karma to post the present version of an RFC made in
response to the GitHub issue found at
https://github.com/php/php-src/issues/9208
Though any guidance or contributions from those more familiar with this
portion of the codebase is more than welcome; we are happy to assign work
implementing this from our end. We would like to gauge interest, feedback
and any brainstorming prior to beginning work in earnest.
Thanks,
Garet
Hi
I am requesting RFC karma to post the present version of an RFC made in
response to the GitHub issue found at
https://github.com/php/php-src/issues/9208
You should have already been granted RFC karma yesterday by derick, he
told me that the Wiki would send a notification.
Can you check whether you are already able to create the RFC page and if
or if not you received an automated email about the permission adjustment?
Best regards
Tim Düsterhus
You are correct, thank you.
The RFC draft has been posted to
https://wiki.php.net/rfc/treat_enum_instances_as_values
-Garet
Hi
I am requesting RFC karma to post the present version of an RFC made in
response to the GitHub issue found at
https://github.com/php/php-src/issues/9208You should have already been granted RFC karma yesterday by derick, he
told me that the Wiki would send a notification.Can you check whether you are already able to create the RFC page and if
or if not you received an automated email about the permission adjustment?Best regards
Tim Düsterhus
Hello everyone,
Would this change affect WeakMap somehow?
Regards,
Dimitar
You are correct, thank you.
The RFC draft has been posted to
https://wiki.php.net/rfc/treat_enum_instances_as_values-Garet
Hi
I am requesting RFC karma to post the present version of an RFC made in
response to the GitHub issue found at
https://github.com/php/php-src/issues/9208You should have already been granted RFC karma yesterday by derick, he
told me that the Wiki would send a notification.Can you check whether you are already able to create the RFC page and if
or if not you received an automated email about the permission
adjustment?Best regards
Tim Düsterhus
You are correct, thank you.
The RFC draft has been posted to
https://wiki.php.net/rfc/treat_enum_instances_as_values-Garet
I think this example should be "mixed" instead of "$mixed"? public function offsetGet($mixed $which){
Maybe a bit of a side-track, what about an interface with a method that
lets an object specify what it would turn into if it were used as an array
key? In the case of enums that would mean they'd implement this interface
and just return the value. I hadn't seen this idea come by yet and I don't
know if it's a good idea, just something I was thinking of while trying to
group data objects based on a value. In theory this could also be done with
Stringable instead.
<?php
interface ArrayKeyable {
public function toArrayKey(): string|int;
}
final class MyObject implements ArrayKeyable
{
public function __construct(
public readonly int $id,
public readonly string $name,
) {}
public function toArrayKey(): string {
return $this->name;
}
}
$objects = [];
$data = [[1, 'Jane'], [2, 'John'], [3, 'Jake'], [4, 'Jane']];
foreach($data as [$id, $name]) {
$object = new MyObject($id, $name);
$objects[$object->toArrayKey()][] = $object;
// would turn into $objects[$object][] = $object;
}
var_dump($objects);
You are correct, thank you.
The RFC draft has been posted to
https://wiki.php.net/rfc/treat_enum_instances_as_values-Garet
I have already explained at length on the PR thread why this is a fundamentally wrong way to approach Enums. We absolutely should work to improve the Enum collection story in PHP, but silently casting Enums to scalars is the wrong way to do it.
As noted in the original Enum RFC:
As objects, Enum cases cannot be used as keys in an array. However, they can be used as keys in a SplObjectStorage or WeakMap. Because they are singletons they never get garbage collected, and thus will never be removed from a WeakMap, making these two storage mechanisms effectively equivalent.
WeakMap may not be not ideal, but it is more ideal and effective than introducing silent Enum->primitive casting, which we have explicitly and deliberately avoided until now. It can still be used in mostly the same way as arrays are today; the only caveat would be if you're expecting to use a mix of different types as keys, in which case the answer is to fix the code instead because that's already a broken approach.
--Larry Garfield