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/
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
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/
Hi,
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.
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/
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