Bug #66809 [ReO->Csd]: Inconsistence in using $this variable
| From: | dmitry@php.net | Date: | Wed, 13 Jul 2016 12:44:32 +0000 |
| Subject: | Bug #66809 [ReO->Csd]: Inconsistence in using $this variable | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-202279@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
Updated by: dmitry@php.net
Reported by: o dot pikozh at gmail dot com
Summary: Inconsistence in using $this variable
-Status: Re-Opened
+Status: Closed
Type: Bug
Package: Scripting Engine problem
PHP Version: 5.6.0alpha2
-Assigned To:
+Assigned To: dmitry
Block user comment: N
Private report: N
New Comment:
Fixed in PHP-7.1.
See https://wiki.php.net/rfc/this_var for more
details.
Previous Comments:
------------------------------------------------------------------------
[2014-06-03 13:05:27] 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.
------------------------------------------------------------------------
[2014-06-03 13:03:28] 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 12:59:20] 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 12:47:42] 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: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.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=66809
--
Edit this bug report at https://bugs.php.net/bug.php?id=66809&edit=1