rfc:objkey

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

rfc:objkey [2014/10/22 08:42]
stas
rfc:objkey [2017/09/22 13:28]
Line 1: Line 1:
-====== PHP RFC: Objects as hash keys ====== 
-  * Version: 0.9 
-  * Date: 2014-10-21 
-  * Author: Stas Malyshev (stas@php.net) 
-  * Status: Draft  
-  * First Published at: http://wiki.php.net/rfc/objkey 
  
-The purpose of this RFC is to introduce a magic method that will allow objects to serve as hash keys.  
- 
-===== Introduction ===== 
- 
-In PHP array keys can be only represented as numbers and strings. However, there already are several classes that represent different kinds of numbers (such as GMP objects) and some objects can also represent strings (e.g. Unicode strings) or string-like objects. It would be convenient to be able to use such objects as array keys to. 
- 
-It could be achieved as conversion to string, but this is not ideal since object's string representation does not always match object's identity and there may be case where human-readable string may be different from value for the purposes of hashing. Also, some object may prefer to produce numeric index.  
- 
-===== Proposal ===== 
-Create a new magic method, %%__hash()%% which is called when object is supplied as a hash key, and returns string or integer that is used as the hash key. 
- 
-A number of languages implement the same facility, namely: 
-  * Java has hashCode() and toString() 
-  * Python has %%__str__, __repr__ and __hash__%% 
-  *  Ruby has object.hash 
- 
-The method should produce a value which is acceptable as a key (not including objects),  
-otherwise the engine will still produce an illegal offset type error. The objects not having this 
-method implemented would produce an illegal offset type error when used in hash key position as before. 
- 
-===== Backward Incompatible Changes ===== 
- 
-Should not break anything as we don't allow this now. 
- 
-===== Proposed PHP Version(s) ===== 
-Targeted for PHP 7 
- 
-===== RFC Impact ===== 
-==== To SAPIs ==== 
- 
-No impact on SAPIs 
- 
-==== To Existing Extensions ==== 
- 
-If somebody implements something like ArrayAccess they may want to update it to accommodate objects. 
- 
-==== To Opcache ==== 
- 
-Since objects are run-time, should not have any effects on opcache. 
- 
-==== New Constants ==== 
- 
-None. 
- 
-==== php.ini Defaults ==== 
- 
-None. 
- 
-===== Open Issues ===== 
- 
-Should this be supported for strings? I don't think it makes much sense but mentioning it here for completeness sake. 
- 
- 
-===== Proposed Voting Choices ===== 
- 
-New magic method requires 2/3 majority. 
- 
-===== Patches and Tests ===== 
- 
-TBD 
- 
-===== Implementation ===== 
- 
-TBD 
- 
-===== References ===== 
-Discussed here: 
- 
-http://marc.info/?t=141145969600001&r=1&w=2 
-===== Rejected Features ===== 
- 
-Using %%_toString%% for the key conversion, for the resons discussed above.  
rfc/objkey.txt · Last modified: 2017/09/22 13:28 (external edit)