rfc:incompat_ctx

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
rfc:incompat_ctx [2012/07/30 20:34] – The change cataphractrfc:incompat_ctx [2013/09/25 20:17] – note that deprecation is implemented nikic
Line 5: Line 5:
   * Date: 2012-07-30   * Date: 2012-07-30
   * Author: Gustavo Lopes   * Author: Gustavo Lopes
-  * Status: Under Discussion+  * Status: Deprecation implemented in PHP 5.6
   * First Published at: http://wiki.php.net/rfc/incompat_ctx   * First Published at: http://wiki.php.net/rfc/incompat_ctx
  
Line 58: Line 58:
  
 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 <php>$this->method()</php> instead of <php>OrigClass::method()</php>. Example:
 +
 +<code php>
 +<?php
 +class A {
 + function dumpClass() {
 + var_dump(get_class($this));  
 + }
 +}
 +class B {
 + function test() {
 + A::dumpClass();
 + }
 +}
 +
 +$a = new A;
 +$b = new B;
 +$a->dumpClass();
 +$b->test();
 +</code>
 +
 +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->dumpClass();
 + }
 +}
 +
 +$a = new A;
 +$b = new B;
 +$a->dumpClass();
 +$b->test();
 +</code>
 +
 +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::dumpClass($this); //E_STRICT ($this would be NULL on callee)
 + }
 +}
 +
 +$a = new A;
 +$b = new B;
 +$a->dumpClass();
 +$b->test();
 +</code>
 +
 +===== Vote =====
 +
 +Voting ends not before Monday, January 28th 2013. The PHP language is ultimately changed, so a 2/3 majority is required.
 +
 +<doodle 
 +title="Deprecate calls with incompatible context in 5.5 and disallow them in the version after (be it 5.6 or 6.0)" auth="cataphract" voteType="single" closed="True">
 +   * Yes
 +   * No
 +</doodle>
  
 ===== 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.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1