Re: Missing __toString() part
| From: | Rasmus Lerdorf | Date: | Sat, 03 Jun 2006 17:11:58 +0000 |
| Subject: | Re: Missing __toString() part | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-23891@lists.php.net to get a copy of this message | ||
Right, just let me clarify. My point here was that if we are going to make a change here, we should do it correctly and come up with a way to create a unique hash of the object so it really can be used as an array index. Magically calling __toString() in a case where we don't have an explicit string context sounds half-assed and non-obvious to me. If you want that ability, simply do: $a[(string)$obj] and it is obvious what you are doing.
-Rasmus
Marcus Boerger wrote:
Hello Rasmus, though you are probably right here your idea brought to it's full extend will bring a major BC break. Just consider we had both key and value as a zval then the current behavior would change for quite a lot of situations including not only objects and resources but also booleans, floats and NULL. Right now everybody is used to the restiction that an array key can only be a string or an integer and if not it is either not acceptable as a key at all or is being converted to either one. best regards marcus Saturday, June 3, 2006, 6:30:38 PM, you wrote:I don't understand why using the object as an index would trigger a __toString() call. PHP's array indices are not defined to be strings, so I don't see this as being a string context use and thus __toString() shouldn't be called.I also don't see why we need to make a distinction between the key and the value here if there is any way we can avoid it.For example, this obviously works:class foo {function a() { echo "a"; }} $a = new foo(); $b[1] = $a; $b[1]->a();Why shouldn't this?$b[$a] = 1; key($b)->a();I have always looked a PHP array as having 2 places to store things. You can store stuff in the key with the caveat/feature that it must be unique and you can store whatever you want in the value part. Only allowing strings and integers in the key seems like an arbitrary restriction to me. As long as the key is something we can somehow uniquely identify I think we should aim to support it.-RasmusIlia Alshanetsky wrote:Best regards, MarcusI think this is a potentially missing bit of functionality we definitely need to consider including. There is really no technical reason why $foo[new ToStringObject] = "bar"; cannot work or a reason why it shouldn't as far as functionality goes. Ilia Alshanetsky On 3-Jun-06, at 7:42 AM, Marcus Boerger wrote:Hello guys, the attached patch closes one more __toString() part. It allows to use objects that define __toString as indexes to arrays. What do you guys think about this, should we add it or stay with the old behavior that didn't allow objects as indexes at all. --Best regards,--PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.phpMarcus mailto:mail@marcus-boerger.de<ze2-offset-obj-tostring-20060603.diff.txt> --PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php