Accessing private variables from within a closure

musicking

New Member
I'm trying to reference a private variable of an object from within a closure. The code below would seem to work, but it complains \[code\]Fatal error: Cannot access self:: when no class scope is active in test.php on line 12\[/code\] and \[code\]Fatal error: Using $this when not in object context in test.php on line 20\[/code\].Any ideas how to accomplish the same results using a closure while keeping the variables private and without making helper functions (defeating the whole idea of a private variable).\[code\]class MyClass{ static private $_var1; private $_var2; static function setVar1( $value ) { $closure = function () use ( $value ) { self::$_var1 = $value; }; $closure(); } function setVar2( $value ) { $closure = function () use ( $value ) { $this->_var2 = $value; }; $closure(); }}MyClass::setVar1( "hello" ); //doesn't work$myclass = new MyClass;$myclass->setVar2( "hello" ); //doesn't work\[/code\]
 
Back
Top