rfc:this_var

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:this_var [2016/05/23 20:15] – created dmitryrfc:this_var [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2016-05-23   * Date: 2016-05-23
   * Author: Dmitry Stogov, dmitry@zend.com   * Author: Dmitry Stogov, dmitry@zend.com
-  * Status: Under Discussion+  * Status: Accepted
   * First Published at: https://wiki.php.net/rfc/this_var   * First Published at: https://wiki.php.net/rfc/this_var
  
Line 40: Line 40:
 ==== Disable using $this as static variable ==== ==== Disable using $this as static variable ====
  
-The following code worked in PHP 7, but will emit compilation error in PHP 7.1+The following code leaded to "Cannot re-assign $this" compilation error. In PHP 7.1 it will produce more suitable error message - "Cannot use $this as static variable".
  
 <code php> <code php>
Line 91: Line 91:
 <code php> <code php>
 $a = "this"; $a = "this";
-$aa = 42; // throw new Error("Cannot re-assign $this")+$$a = 42; // throw new Error("Cannot re-assign $this")
 </code> </code>
  
Line 111: Line 111:
 $x->foo(); $x->foo();
 </code> </code>
 +
 +==== Disable ability to re-assign $this indirectly through extract() and parse_str() ====
 +
 +Few internal PHP functions may re-assign local variable. In PHP 7.1 they cannot change value of $this variable and throw Error exception.
 +
 +<code php>
 +class C {
 +  function foo(){
 +    extract(["this"=>42]);  // throw new Error("Cannot re-assign $this")
 +    var_dump($this);
 +  }
 +}
 +$x = new C;
 +$x->foo();
 +</code>
 +
 +==== get_defined_vars() always doesn't show value of variable $this ====
 +
 +In PHP 7.0 and below get_defined_vars() might show or not show value of $this depending on some condition. (e.g. it was shown if we used $this variable itself, but not if it was used in a $this property reference or method call). In PHP 7.1 we won't show the value of $this in all cases.
  
 ==== Always show true $this value in magic method __call() ==== ==== Always show true $this value in magic method __call() ====
Line 122: Line 141:
     $this->test();   // prints "ops"     $this->test();   // prints "ops"
   }   }
-  funcriont test() {+  function test() {
     echo "ops\n";      echo "ops\n"; 
   }   }
Line 128: Line 147:
 $x = new C; $x = new C;
 $x->foo(); $x->foo();
 +</code>
 +
 +==== Using $this when not in object context ====
 +
 +Attempt to use $this in plain function or method now will lead to exception "Using $this when not in object context". This unifies behavior with method call and property access. Previously PHP emitted "undefined variable" notice and continued execution assuming $this is NULL. It's still possible to use isset($this) and empty($this) to check object context.
 +
 +<code php>
 +function foo() {
 +    var_dump($this); // throws "Using $this when not in object context"
 +                     // php-7.0 emitted "Undefined variable: this" and printed NULL
 +}
 +foo();
 </code> </code>
  
Line 151: Line 182:
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
 The vote is a straight Yes/No vote, that requires a 2/3 majority. The vote is a straight Yes/No vote, that requires a 2/3 majority.
-The voting will begin on Jun 6 and will close on Jun 16.+The voting began on Jun 6 and will close on Jun 16. 
 +<doodle title="Fix inconsistent behavior of $this variable?" auth="dmitry" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 159: Line 194:
 After the project is implemented, this section should contain  After the project is implemented, this section should contain 
   - the version(s) it was merged to   - the version(s) it was merged to
-  - a link to the git commit(s)+  - a link to the git commit [[ http://git.php.net/?p=php-src.git;a=commitdiff;h=a9512af8109e889eb2c6042c57797184930667cd|a9512af8109e889eb2c6042c57797184930667cd]]
   - a link to the PHP manual entry for the feature   - a link to the PHP manual entry for the feature
- 
-===== Rejected Features ===== 
-Keep this updated with features that were discussed on the mail lists. 
rfc/this_var.1464034521.txt.gz · Last modified: 2017/09/22 13:28 (external edit)