rfc:incompat_ctx
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:incompat_ctx [2012/07/30 17:20] – created cataphract | rfc:incompat_ctx [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Request for Comments: | + | ====== Request for Comments: |
+ | An RFC for deprecating and removing < | ||
* Version: 1.0 | * Version: 1.0 | ||
* Date: 2012-07-30 | * Date: 2012-07-30 | ||
* Author: Gustavo Lopes | * Author: Gustavo Lopes | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
+ | ===== Introduction ===== | ||
- | An RFC for deprecating and removing | + | This RFC proposes |
- | ===== Introduction | + | ===== What this proposal is not about ===== |
- | This RFC proposes deprecating (in PHP 5.5) and removing | + | This proposal is not about removing |
- | ==== The feature ==== | + | ===== The feature |
To be clear, the feature I'm proposing to remove is this: | To be clear, the feature I'm proposing to remove is this: | ||
- | <php> | + | <code php> |
class A { | class A { | ||
function foo() { var_dump(get_class($this)); | function foo() { var_dump(get_class($this)); | ||
Line 26: | Line 29: | ||
$b = new B; | $b = new B; | ||
$b-> | $b-> | ||
- | </php> | + | </code> |
Internal methods cannot be called in this fashion. | Internal methods cannot be called in this fashion. | ||
+ | |||
+ | ===== The Change ==== | ||
+ | |||
+ | Even though I think an error at call site would be the most useful to the user, the most sensible option is to just have < | ||
+ | |||
+ | <code php> | ||
+ | class A { function foo() {} } | ||
+ | A::foo(); // E_STRICT | ||
+ | </ | ||
===== Rationale ===== | ===== Rationale ===== | ||
- | Method implementations almost always assume that $this refers to an instance of a compatible type. When they are called with an incompatible | + | Method implementations almost always assume that $this refers to an instance of a compatible type. When they are called with an incompatible |
Because this feature is surprising and little-known, | Because this feature is surprising and little-known, | ||
Line 38: | Line 50: | ||
When combined with LSB, it can be even more difficult to identify. See https:// | When combined with LSB, it can be even more difficult to identify. See https:// | ||
- | |||
- | ===== What this proposal is not about ===== | ||
- | |||
- | This proposal is not about removing static calls to instance methods or instance-like calls to static methods. | ||
===== BC break ===== | ===== BC break ===== | ||
- | The break should be minor. I very much doubt there are many lines of code that rely of it. It has been discouraged with an E_STRICT | + | The break should be minor. I very much doubt there are many lines of code that rely on this feature. It has been discouraged with an <php>E_STRICT</ |
- | This feature, as noted in the [comment](http:// | + | This feature, as noted in the [[http:// |
This feature can, however, be used to implement trait-like behavior, and I'm sure someone somewhere did such a thing. | This feature can, however, be used to implement trait-like behavior, and I'm sure someone somewhere did such a thing. | ||
+ | |||
+ | ===== Alternatives ===== | ||
+ | |||
+ | Using traits is perhaps the easiest and cleanest way to replace code that relies on the feature to be removed. Changing the code to use traits implies: 1) refactoring the instance methods called from incompatible contexts into a trait, 2) make the callers from incompatible contexts use the new trait, 3) change the call sites to use < | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | class A { | ||
+ | function dumpClass() { | ||
+ | var_dump(get_class($this)); | ||
+ | } | ||
+ | } | ||
+ | class B { | ||
+ | function test() { | ||
+ | A:: | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $a = new A; | ||
+ | $b = new B; | ||
+ | $a-> | ||
+ | $b-> | ||
+ | </ | ||
+ | |||
+ | would become: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | trait ATrait { | ||
+ | function dumpClass() { | ||
+ | var_dump(get_class($this)); | ||
+ | } | ||
+ | } | ||
+ | class A { | ||
+ | use ATrait; | ||
+ | } | ||
+ | class B { | ||
+ | use ATrait; | ||
+ | function test() { | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $a = new A; | ||
+ | $b = new B; | ||
+ | $a-> | ||
+ | $b-> | ||
+ | </ | ||
+ | |||
+ | A worse solution, which relies on the possibility of calling instance methods statically would be using an extra parameter: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | class A { | ||
+ | function dumpClass($obj=null) { | ||
+ | if ($obj === null) | ||
+ | $obj = $this; | ||
+ | var_dump(get_class($obj)); | ||
+ | } | ||
+ | } | ||
+ | class B { | ||
+ | function test() { | ||
+ | A:: | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $a = new A; | ||
+ | $b = new B; | ||
+ | $a-> | ||
+ | $b-> | ||
+ | </ | ||
+ | |||
+ | ===== Vote ===== | ||
+ | |||
+ | Voting ends not before Monday, January 28th 2013. The PHP language is ultimately changed, so a 2/3 majority is required. | ||
+ | |||
+ | < | ||
+ | title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Changelog ===== | ===== Changelog ===== | ||
* 2012-07-30: Initial version | * 2012-07-30: Initial version | ||
+ | * 2013-01-20: Opened vote | ||
+ | * 2013-01-28: Closed vote; RFC accepted unanimously with 15 votes in favor |
rfc/incompat_ctx.1343668838.txt.gz · Last modified: 2017/09/22 13:28 (external edit)