rfc:autoboxing
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:autoboxing [2010/05/04 03:52] – Add discussions moriyoshi | rfc:autoboxing [2011/04/06 10:59] – external edit 127.0.0.1 | ||
---|---|---|---|
Line 13: | Line 13: | ||
===== Why do we find it useful in PHP? ===== | ===== Why do we find it useful in PHP? ===== | ||
- | In PHP, unlike other scripting languages, there are fundamental differences between primitive types and classes. | + | In PHP, unlike other scripting languages, there are fundamental differences between primitive types and classes. |
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | $sum = $arr-> | ||
+ | $sum = array_sum(array_keys($arr)); | ||
+ | ?> | ||
+ | </ | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 26: | Line 33: | ||
</ | </ | ||
- | To enable autoboxing on integer | + | To enable autoboxing on integer |
<code php> | <code php> | ||
Line 50: | Line 57: | ||
// Test code | // Test code | ||
- | var_dump(1-> | + | $val = 1; |
+ | var_dump($val-> | ||
+ | ?> | ||
+ | </ | ||
+ | To take advantage of this autoboxing feature, I also propose a modification to the language syntax that enables arbitrary values to come to the left-hand side of arrow operators ('' | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | // Test code | ||
+ | var_dump(1-> | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | You may find it interesting that it would also allow you to write an arrow operator right after a new operator and parenthesized parameters. | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | new Foo()-> | ||
?> | ?> | ||
</ | </ | ||
===== Discussions ===== | ===== Discussions ===== | ||
- | | + | ==== Conflicts between libraries that utilizes this feature ==== |
- | * Per-namespace autoboxing rules (like extension methods in C#) | + | [[http:// |
+ | > I fear that a lot of people will (have) to come up with their own string libraries, int libraries etc. which will have (subtle) differences. Guess you code for two projects, one using the autbox library X and one library Y. Would you always remember which methods you're allowed to call on an int, or the order of parameters, especially when there are many methods which have a similar name? | ||
+ | |||
+ | [[http:// | ||
+ | > It looks like a very interesting idea. However, I believe that we should learn from the %%__autoload%% experience: It does not cope well with multiple projects. I'd rather have register_autoboxer($callback) or even register_autoboxer(" | ||
+ | |||
+ | [[http:// | ||
+ | > In general I would agree that allowing multiple instances of %%__auto*%% is a good thing, but with %%__autoload%% you are loading class names that have far more diversity than basic types. | ||
+ | |||
+ | === Possible fix === | ||
+ | * Per-namespace autoboxing rules (like extension methods in C#) -- The idea is to allow namespaces to have each '' | ||
+ | |||
+ | ==== Relation to the existing PECL libraries ==== | ||
+ | [[http:// | ||
+ | > Is there any reason why primitives couldn' | ||
+ | |||
+ | [[http:// | ||
+ | > I liken this to pecl/ | ||
+ | |||
+ | ==== Efficiency ==== | ||
+ | [[http:// | ||
+ | > I am afraid, this magic method will make php slower even if scripts don't use this future (at least the patch | ||
+ | |||
+ | ==== Magic functions considered harmful ==== | ||
+ | [[http:// | ||
+ | > Introducing new magic function may bring a lot of troubles and open a new door for exploit writer (we already have problems with __toString() method((See [[http:// | ||
+ | ]]))). | ||
+ | |||
+ | ==== Context information to the callback ==== | ||
+ | [[http:// | ||
+ | > Should' | ||
+ | |||
+ | ==== Exception on infeasible conversion ==== | ||
+ | [[http:// | ||
+ | > It could throw an exception. | ||
===== Patch ===== | ===== Patch ===== | ||
A preliminary patch (may be a bit outdated) is available at http:// | A preliminary patch (may be a bit outdated) is available at http:// | ||
+ | |||
+ | ===== Discussion on the List ===== | ||
+ | |||
+ | - 2005-02-09 : [[http:// | ||
+ | - 2010-05-03 : [[http:// | ||
===== Changelog ===== | ===== Changelog ===== | ||
+ | 2010-05-04: initial version | ||
- | 2008-10-13: initial version |
rfc/autoboxing.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1