php - Use a class static variable as part of a closure use list -
i "use" static class variable part use list statement of closure ?
following snippets fail unexpected 'self' parse errors.
array_walk($_categories, function($c, $i) use (&self::$tree) {
or
array_walk($_categories, function($c, $i) use (self::&$tree) {
parse error: syntax error, unexpected 'self' (t_string), expecting variable (t_variable)
is there special syntax use in special case ?
why on earth want that? given use of self
, closure defined within class somewhere, you're able access static member anyway:
class foo { protected static $bar = 123; public function test() { return function($x) { static::$bar += $x; // or self::$bar return static::$bar; }; } } $x = new foo; $y = $x->test(); var_dump($y(1));//int(124) var_dump($y(2));//int(126)
no need muck references @ all...
if you're on eol'ed version of php (5.3 example), work around problem assigning reference static member first, , pass reference reference via use
:
public function test() { $staticref = &static::$bar; return function($x) use (&$staticref) { $staticref += $x; return $staticref; }; }
but if you're still working version of php eol'ed long ago, should upgrading...
Comments
Post a Comment