rfc:hashkey
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:hashkey [2014/10/22 12:21] – created krakjoe | rfc:hashkey [2014/10/22 17:57] – krakjoe | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: __hashKey | + | ====== PHP RFC: __toKey |
* Version: 0.9 | * Version: 0.9 | ||
* Date: 2014-10-22 | * Date: 2014-10-22 | ||
Line 5: | Line 5: | ||
* Status: Draft | * Status: Draft | ||
* First Published at: http:// | * First Published at: http:// | ||
- | |||
- | ===== Introduction ===== | ||
- | Often discussed is the problem that we cannot use objects as array keys. | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | We introduce a new magic __hashKey | + | A new magic toKey method, to resolve the problem of not being able to use Objects as array keys. |
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | class Foo { | ||
+ | public function __toKey() { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $foo = new Foo(); | ||
+ | $test = [ | ||
+ | $foo => true | ||
+ | ]; | ||
+ | </ | ||
+ | |||
+ | Inheritance shall work as any other magic method: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | class Foo { | ||
+ | public function __toKey() { | ||
+ | return " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | class Bar extends Foo { | ||
+ | /* shall use Foo:: | ||
+ | } | ||
+ | |||
+ | $bar = new Bar(); | ||
+ | $test = [ | ||
+ | $bar => true | ||
+ | ]; | ||
+ | </ | ||
+ | |||
+ | Returning a non-scalar shall fail as it did before: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | class Foo { | ||
+ | public function __toKey() { | ||
+ | return []; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $foo = new Foo(); | ||
+ | $test = [ | ||
+ | $foo => true | ||
+ | ]; | ||
+ | </ | ||
+ | |||
+ | Shall yield: | ||
+ | |||
+ | <code php> | ||
+ | Warning: Illegal offset type in %s on line %d | ||
+ | </ | ||
- | Error conditions are _____ | + | The current behaviour of __toString is unchanged. |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== |
rfc/hashkey.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1