php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66809 Inconsistence in using $this variable
Submitted: 2014-03-02 09:03 UTC Modified: 2016-07-13 12:44 UTC
From: o dot pikozh at gmail dot com Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.6.0alpha2 OS:
Private report: No CVE-ID: None
 [2014-03-02 09:03 UTC] 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.




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-04-13 20:42 UTC] tyrael@php.net
for the reference, I made a similar report a couple of years back:
http://bugs.php.net/52428
 [2014-06-03 07:27 UTC] tyrael@php.net
-Status: Open +Status: Duplicate
 [2014-06-03 07:27 UTC] tyrael@php.net
closing at as duplicate
 [2014-06-03 11:24 UTC] 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 12:36 UTC] tyrael@php.net
-Status: Duplicate +Status: Re-Opened
 [2014-06-03 12:36 UTC] 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 12:47 UTC] o dot pikozh at gmail dot com
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();});
 [2014-06-03 12:59 UTC] o dot pikozh at gmail dot com
Hmmm, I've realized that this now can be implemented even without creating explicit '$this' parameter (since PHP 5.4 supports bind/bindTo).


Never-the-less, per my opinion:

- Either this code should run fine:
    function f($this) {return $this->m;}
    f($someObject);

- Either this code should fail:
    function f($this) {}
    //I.e. just declaring function with '$this' parameter should cause error.
 [2014-06-03 13:03 UTC] o dot pikozh at gmail dot com
... but currently "function f($this) {}" (and even "function f($this) {print_r($this);} f($someObject);") runs fine, but "function f($this) {print_r($this->m);} f($someObject);" fails.
 [2014-06-03 13:05 UTC] o dot pikozh at gmail dot com
I.e. that checking for "PHP Fatal error: Using $this when not in object context" should happen not on using "->" operator, but on function declaration.
 [2016-07-13 12:44 UTC] dmitry@php.net
-Status: Re-Opened +Status: Closed -Assigned To: +Assigned To: dmitry
 [2016-07-13 12:44 UTC] dmitry@php.net
Fixed in PHP-7.1.
See https://wiki.php.net/rfc/this_var for more details.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 14:01:32 2024 UTC