On Fri, Apr 28, 2023 at 11:48 PM Garet Claborn <ga...@suiteux.com> wrote:

> 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.

https://3v4l.org/Vi8kY#v8.2.5
```php
<?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);
```

Reply via email to