|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #55038 inconsistent error-handling for $this with closure
Submitted: 2011-06-12 03:53 UTC Modified: 2016-08-31 21:48 UTC
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: rasmus at mindplay dot dk Assigned: cmb (profile)
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.6 OS: Win7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
8 + 47 = ?
Subscribe to this entry?

 [2011-06-12 03:53 UTC] rasmus at mindplay dot dk
$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:

$foo = function($this)
  var_dump($this); // this will work!


// 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


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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-06 10:27 UTC] 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.


$foo = function($this)
  var_dump($this); // this will work!
  var_dump($this->bar); // this will blow up

$foo(new stdclass);


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.
 [2014-07-15 11:22 UTC]
-Summary: inconsistent error-handling for $this +Summary: inconsistent error-handling for $this with closure -Type: Bug +Type: Documentation Problem
 [2014-07-15 11:22 UTC]
Closure is implemented by class. Therefore, this is limitation.
If this is not documented, it should be written somewhere.
 [2016-08-31 21:48 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-08-31 21:48 UTC]
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

The inconsistency will be fixed as of PHP 7.1.0, due to
<>. For anonymous functions $this
is already forbidden as parameter as of PHP 7.0.7, to solve bug
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Dec 02 21:01:27 2023 UTC