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

From: Date: Sat, 07 Jan 2012 09:31:19 +0000
Subject: "Cannot use $this as lexical variable" message still in PHP 5.4
Groups: php.internals 
Request: Send a blank email to internals+get-57262@lists.php.net to get a copy of this message
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/

« previous php.internals (#57262) next »