我正在尝试从闭包中引用对象的私有变量。下面的代码似乎有效,但它会抱怨Fatal error: Cannot access self:: when no class scope is active in test.php on line 12
和Fatal error: Using $this when not in object context in test.php on line 20
。
任何想法如何使用闭包来完成相同的结果,同时保持变量私有而不做辅助函数(打败私有变量的整个想法)。
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
答案 0 :(得分:14)
编辑注意,此答案原本适用于 PHP5.3 及更早版本,现在可能。有关最新信息,请参阅this answer。
这不是直接可行的。特别是,闭包没有相关的范围,因此他们无法访问私有和受保护的成员。
但是,您可以使用引用:
<?php
class MyClass
{
static private $_var1;
private $_var2;
static function setVar1( $value )
{
$field =& self::$_var1;
$closure = function () use ( $value, &$field ) {
$field = $value;
};
$closure();
}
function setVar2( $value )
{
$field =& $this->_var2;
$closure = function () use ( $value, &$field ) {
$field = $value;
};
$closure();
}
}
MyClass::setVar1( "hello" );
$myclass = new MyClass;
$myclass->setVar2( "hello" );
答案 1 :(得分:4)
这可以从PHP 5.4.0开始
class test {
function testMe() {
$test = new test;
$func = function() use ($test) {
$test->findMe(); // Can see protected method
$test::findMeStatically(); // Can see static protected method
};
$func();
return $func;
}
protected function findMe() {
echo " [find Me] \n";
}
protected static function findMeStatically() {
echo " [find Me Statically] \n";
}
}
$test = new test;
$func = $test->testMe();
$func(); // Can call from another context as long as
// the closure was created in the proper context.
答案 2 :(得分:2)
闭包没有$this
或self
的概念 - 它们不以这种方式绑定到对象。这意味着您必须通过use
子句传递变量......类似于:
$_var1 =& self::$_var1;
$closure = function() use ($value, &$_var1) {
$_var1 = $value;
};
$_var2 =& $this->_var2;
$closure = function() use ($value, &$_var2) {
$_var2 = $value;
};
我没有测试过上面的代码,但我认为这是正确的。