Hi! > I’m not sure that’d make much sense. The object isn’t the key, the > value the magic method returns is. It would be quite odd to do this: > > $someArray = [$my__hashImplementingObject => 1]; > var_dump($someArray); > > And see something like this, because we’ve called a hash function: > > array(1) { ["ec10e5a66e281d105f302cacfb1aaca8"]=> int(0) }
The hash doesn't have to be a nonsensical hex value, it can be something like "My Object Number 42" if you want to. The difference is that __toString is for human reading, and it's not always suitable for hashing purposes. Just read the docs on any of the languages that have separate hash method - they all have the same argument, there's a different between printing an object for output and using the object as a key. > I don’t really see what advantage this has over the normal > __toString. Furthermore, having a special method we use to cast here > that’s used nowhere else seems weird. That's the point - it's not a cast. It's an operation that requires object's identity. Again, given that so many languages have it, I don't think it's really that weird. I think it's pretty natural. > Now, if we were to add actual object key support, that I might like. > But if we’re going to keep with just integers and strings, I’d much > prefer to just support __toString here. I think users are smart > enough to understand that PHP arrays only have string or int keys, so > it casts to a string. The problem is you mix here two completely different domains. On one hand, you may want the object to have text representation for output purposes - say, XML object would have XML output as it's string rep. On other hand, I don't think you want to use 100K XML as a key, because using as a key is a completely different issue. Converting to string is a hack that mixes two different problems into one method, and that's why it will lead to problems. The solution for this problem is known and widely used - have a separate hash method. With PHP, you can actually have a luxury of using a human-readable keys while still keeping them nice and clean and independent from string representation. And if you want maximum efficiency, you could switch to ints instead. -- 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