Re: [RFC][VOTE] Objects as Keys
| From: | Andrea Faulds | Date: | Tue, 16 Dec 2014 10:52:19 +0000 |
| Subject: | Re: [RFC][VOTE] Objects as Keys | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-79708@lists.php.net to get a copy of this message | ||
Hi Markus,
> On 16 Dec 2014, at 10:31, Markus Fischer <markus@fischer.name> wrote:
>
> On 16.12.14 09:34, Stanislav Malyshev wrote:
>> I'd like to initiate a vote on "objects as keys" RFC:
>> https://wiki.php.net/rfc/objkey
>
> Am I right this only covers the transformation into the array. Once it's
> in it's essential a array compatible key entity (string/integer) so when
> you var_dump($array) you only get the values of a hash.
>
> In other words: it would not be possible to actually get the object from
> the key, just this string/integer which is supposed to reference it?
Exactly. If I were to do this:
<?php
class Foo {
public $foo;
function __construct($foo) {
$this->foo = $foo;
}
function __toKey() {
return $this->foo;
}
}
$arr = [];
$arr[] = new Foo(1);
var_dump($arr);
I would get this:
array(1) {
[0]=>
int(1)
}
This is the main problem with the RFC: magic, implicit, one-way data loss (object to
integer/string).
I was previously in favour of this, but it’d prevent actual indexing by objects in future, and I
can’t think of any use cases which aren’t better solved by explicitly converting to a
string/integer.
Thanks.
--
Andrea Faulds
http://ajf.me/