Doc #55038 [Opn->Nab]: inconsistent error-handling for $this with closure
| From: | cmb@php.net | Date: | Wed, 31 Aug 2016 21:48:36 +0000 |
| Subject: | Doc #55038 [Opn->Nab]: inconsistent error-handling for $this with closure | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-13868@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55038&edit=1
ID: 55038
Updated by: cmb@php.net
Reported by: rasmus at mindplay dot dk
Summary: inconsistent error-handling for $this with closure
-Status: Open
+Status: Not a bug
Type: Documentation Problem
Package: Class/Object related
Operating System: Win7
PHP Version: 5.3.6
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
As dexen dot devries has already shown, this behavior is not
particularly related to closures, but rather stems from the fact
that $this is a special pseudo-variable that is (only) available
when a method is called from within an object context, see
<http://php.net/manual/en/language.oop5.basic.php>.
The inconsistency will be fixed as of PHP 7.1.0, due to
<https://wiki.php.net/rfc/this_var>. For
anonymous functions $this
is already forbidden as parameter as of PHP 7.0.7, to solve bug
#71737.
Previous Comments:
------------------------------------------------------------------------
[2014-07-15 11:22:00] yohgaki@php.net
Closure is implemented by class. Therefore, this is limitation.
If this is not documented, it should be written somewhere.
------------------------------------------------------------------------
[2012-09-06 10:27:28] dexen dot devries at gmali dot com
Note, the closure and creation of another object instance are irrelevant. Two simpler reproduction
scripts -- one uses anonymous function, the other plain function.
<?php
$foo = function($this)
{
var_dump($this); // this will work!
var_dump($this->bar); // this will blow up
};
$foo(new stdclass);
?>
<?php
function bar($this)
{
var_dump($this); // this will work!
var_dump($this->bar); // this will blow up
}
bar(new stdclass);
?>
Expected result: both uses of $this and $this->bar behave in similar way.
Actual result: PHP complains only on $this->bar.
------------------------------------------------------------------------
[2011-06-12 03:53:57] rasmus at mindplay dot dk
Description:
------------
$this is not generally allowed outside an object context, but sometimes it is.
I discovered this while trying to write a cheeky little base-class that would
allow you to decorate objects with new methods, at run-time.
I would have gotten away with it, too - and I still could of course, only I would
have to break from the convention of $this, and naming the context-object
something else, which kind of sucks.
Test script:
---------------
<?php
$foo = function($this)
{
var_dump($this); // this will work!
};
$foo('bar');
// now an object:
class Test
{
public $foo = 'bar';
}
$test = new Test;
// and another closure:
$ouch = function($this)
{
var_dump($this->bar); // this will blow up
};
$ouch($test);
Expected result:
----------------
The two examples should either fail consistently, or succeed consistently.
From my point of view, why should I not be allowed to have a local variable named
$this if I wanted to? There's nothing special or magical about this variable,
besides the fact that it gets automatically assigned on call.
Actual result:
--------------
The second example fails.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55038&edit=1