Bug #66809 [ReO]: Inconsistence in using $this variable

From: Date: Tue, 03 Jun 2014 13:05:27 +0000
Subject: Bug #66809 [ReO]: Inconsistence in using $this variable
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-186031@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: 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. Previous Comments: ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ [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). ------------------------------------------------------------------------ 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

« previous php.bugs (#186031) next »