Bug #66809 [ReO]: Inconsistence in using $this variable
| From: | o dot pikozh at gmail dot com | Date: | Tue, 03 Jun 2014 12:47:43 +0000 |
| Subject: | Bug #66809 [ReO]: Inconsistence in using $this variable | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-186028@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66809&edit=1
ID: 66809
User updated by: o dot pikozh at gmail dot com
Reported by: o dot pikozh at gmail dot com
Summary: Inconsistence in using $this variable
Status: Re-Opened
Type: Bug
Package: Scripting Engine problem
PHP Version: 5.6.0alpha2
Block user comment: N
Private report: N
New Comment:
IMO, just allow function parameter to be called '$this' (not in method, of course). It is
useful for writing systems, which allow dynamic method addition. Like this:
class User extends \MyLib\Core\DynamicallyExtendableObject {
function getSurname() {...}
function getName() {...}
}
...
User::RegisterMethod('getFullname', function($this) {return $this->getName() . '
' . $this->getSurname();});
Previous Comments:
------------------------------------------------------------------------
[2014-06-03 12:36:58] tyrael@php.net
the root cause is the same/similar, we have basic checks in place which tries to catch any variable
reference to $this when not in object context or when it would overwrite $this, but these checks are
not perfect (as otherwise it would cause a more noticable performance loss on every variable
read/write) which causes symptoms like this.
let's see if somebody has some idea about this exact situation, but I'm afraid that this
won't be fixed for the same reasons as the other report.
------------------------------------------------------------------------
[2014-06-03 11:24:13] o dot pikozh at gmail dot com
This is not an exact duplicate.
That report was in fact about that '$GLOBALS["this"]' can be assigned using
'$GLOBALS["this"] = ...', but cannot be assigned using 'global $this'
or '$this = ...'.
This report is in fact about that if you set parameter name to 'this', you somewhy cannot
read its members (although it is not forbidden to set parameter name per se, and to read or copy
parameter value itself).
------------------------------------------------------------------------
[2014-06-03 07:27:59] tyrael@php.net
closing at as duplicate
------------------------------------------------------------------------
[2014-04-13 20:42:08] tyrael@php.net
for the reference, I made a similar report a couple of years back:
http://bugs.php.net/52428
------------------------------------------------------------------------
[2014-03-02 09:03:01] o dot pikozh at gmail dot com
Description:
------------
It is not clearly defined if user is allowed to name his variables '$this'.
(Yep, I know that '$this' variable has special meaning, however its usage in non-special
meaning should be clarified:
- either '$this' variable can never be used in non-special meaning;
- either '$this' variable can be used in non-special meaning on
Test script:
---------------
//Assuming we have:
class TestClass {function m() {echo 'm'};}
//Case 1:
function f1($this) {$this->m();}
//This code compiles.
//Case 2:
function f2($this) {$this->m();}
f2(new TestClass);
//This code sais: 'PHP Fatal error: Using $this when not in object context in'
//Case 3:
function f3($this) {$t = $this; $t->m();}
f3(new TestClass);
//This code compiles and says 'm'.
This is inconsistent.
Either it should fail immediately after reading 'function fN($this)' saying smth like
'Fatal error: Cannot use $this as custom variable/parameter name'.
Or it should work.
At least, cases 2 and 3 should behave identically.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66809&edit=1