Re: [RFC] Using objects as keys
| From: | Will Fitch | Date: | Thu, 30 Oct 2014 19:51:49 +0000 |
| Subject: | Re: [RFC] Using objects as keys | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-78483@lists.php.net to get a copy of this message | ||
> On Oct 30, 2014, at 1:32 PM, Stas Malyshev <smalyshev@sugarcrm.com> wrote:
>
> Hi!
>
>> Put another way, I think a key question here is:
>>
>> class Foo {
>> public $bar;
>> }
>>
>> $a = new Foo;
>> $a->bar = 'baz';
>> $b = new Foo;
>> $b->bar = 'baz';
>>
>> $arr[$a] = true;
>> $arr[$b] = true;
>>
>>
>> Does $arr now contain one value or two?
>
> That depends on the semantics of class Foo. If Foo is something like
> UString, then it should contain one value, since UString is a value
> object (https://en.wikipedia.org/wiki/Value_object) - or at least it
> should be. However, if Foo represents something having separate identity
> - i.e. it's a Person class and 'bar' represents name - then of course it
> should contain two values, since the name is not the sole source of
> Person's identity. So the decision is on you as a programmer.
>
> And to give you the tool to make this decision and let PHP engine know
> about is exactly the point of this RFC.
My only concern at this point is the default value of the hash. If we were to use spl _object_hash,
we could be setting a precedence that a hash must be unique to each object.
Any thoughts on that?
>
>> If the desire is for it to contain 2, then spl_object_hash($a) already
>> has that covered, I think.
>>
>> If the desire is for it to contain 1, then the proposal sounds like a
>> way to normalize Foo::stringifiedValueObjectEquivalent().
>
> You can describe it as such, but in a proper standartized way and with
> syntax that allows you to use same syntax constructs on all keys and not
> have to check each time if it's a special value and call a special
> function on it (same purpose as for __toString).
> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>