# "Cannot use $this as lexical variable" message still in PHP 5.4

- Thread: https://externals.io/message/57262
- Original archive: https://news-web.php.net/php.internals/57262
- Started by Sebastian Bergmann on 2012-01-07T09:31:19+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #57262 — Sebastian Bergmann — 2012-01-07T09:31:19+00:00

URL: https://externals.io/message/57262#57262

While

     <?php
     class a {
         public function b() {
             $f = function () {
                 $this->c();
             };
         }
     }

  compiles as expected with PHP 5.4, the following

     <?php
     class a {
         public function b() {
             $f = function () use ($this) {
                 $this->c();
             };
         }
     }

  results in

    Fatal error: Cannot use $this as lexical variable in ...

  This is because

     void zend_do_fetch_lexical_variable(znode *varname,
     zend_bool is_ref TSRMLS_DC) /* {{{ */
     {
         znode value;

         if (Z_STRLEN(varname->u.constant) == sizeof("this") - 1 &&
             memcmp(Z_STRVAL(varname->u.constant), "this",
             sizeof("this") - 1) == 0) {
             zend_error(E_COMPILE_ERROR, "Cannot use $this as lexical
             variable");
             return;
         }

  is still in zend_compile.c. IIRC, this code was added in PHP 5.3 to
  prevent $this from being used as a lexical variable. It should no
  longer be required in PHP 5.4. Right?

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #57263 — Stas Malyshev — 2012-01-07T09:34:39+00:00

URL: https://externals.io/message/57262#57263 · In reply to: #57262

Hi!

>    is still in zend_compile.c. IIRC, this code was added in PHP 5.3 to
>    prevent $this from being used as a lexical variable. It should no
>    longer be required in PHP 5.4. Right?

Why you need to add $this there? $this should be available automatically 
IIRC unless you make the closure static.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #57264 — Sebastian Bergmann — 2012-01-07T09:40:16+00:00

URL: https://externals.io/message/57262#57264 · In reply to: #57263

Am 07.01.2012 10:34, schrieb Stas Malyshev:
> Why you need to add $this there? $this should be available automatically
> IIRC unless you make the closure static.

 That is not the point I wanted to make. Explicitly listing $this in
 use() should not trigger a compiler error, IMHO.

 I just wanted to make sure that it was not an oversight to not remove
 the error message.

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #57265 — Etienne Kneuss — 2012-01-07T14:16:36+00:00

URL: https://externals.io/message/57262#57265 · In reply to: #57264

Hi,


>
> Am 07.01.2012 10:34, schrieb Stas Malyshev:
> > […]
>
>  That is not the point I wanted to make. Explicitly listing $this in
>  use() should not trigger a compiler error, IMHO.

To me it seems similar to assigning $this, only here it is via special
closure syntax. Since this code is no longer necessary, and might conflict
with the rebinding of $this, the error makes sense IMHO.

>
>  I just wanted to make sure that it was not an oversight to not remove
>  the error message.
>
> --
> Sebastian Bergmann                    Co-Founder and Principal Consultant
> http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #57328 — Daniel Convissor — 2012-01-10T18:20:56+00:00

URL: https://externals.io/message/57262#57328 · In reply to: #57265

Hi Etienne:

> To me it seems similar to assigning $this, only here it is via special
> closure syntax. Since this code is no longer necessary, and might conflict
> with the rebinding of $this, the error makes sense IMHO.

A clearer error message seems appropriate.  It should indicate that
use($this) is inappropriate, but that $this can be used.  Something
_like_ "$this is available without being declared."

Thanks,

--Dan

-- 
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
            data intensive web and database programming
                http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409
