dec*_*eze 6 php anonymous-function
拿一个人为的例子,我想protected static从另一个上下文通过回调函数调用一个方法:
class Foo {
protected static function toBeCalled() { }
public static function bar() {
functionThatAcceptsACallback(function () {
self::toBeCalled();
});
}
}
Run Code Online (Sandbox Code Playgroud)
这在PHP 5.3中是否可行?我找不到让它工作的方法......
这是不可能的,但它将在5.4中以及$this在闭包中的支持.
添加了封闭$这个支持.(STA)的
编辑
这适用于5.4alpha1.
class A
{
private function y()
{
print "y".PHP_EOL;
}
static private function z()
{
print "z".PHP_EOL;
}
function x()
{
return function() {
$this->y();
self::z();
};
}
}
$class = new A();
$closure = $class->x();
$closure();
/*
Output:
y
z
*/
Run Code Online (Sandbox Code Playgroud)