在命名空间之间作为参数传递的匿名函数


Anonymous Function Passed as Argument Between Namespaces

下面的代码导致PHP抛出一个错误:

namespace NamespaceOne;
class MyClass {
    function __construct( array $config ) {
        $func = $config['func'];
        $value = 'Hello World';
        echo $func( $value );   // This part throws the error
    }
}

类在另一个文件中实例化:

namespace NamespaceTwo;
$class = new 'NamespaceOne'MyClass( array(
    'func' => function( $v ) { return $v; }
));

以错误结束:

Fatal error: Function name must be a string [...]

编辑

如果我在命名空间内重新声明函数,它可以工作:

class MyClass {
    function __construct( array $config ) {
        $config['func'] = function( $v ) { return $v; };
        $func = $config['func'];
        $value = 'Hello World';
        echo $func( $value );   // Echos "Hello World"
    }
}

现在我们知道是什么导致了它的中断,但是我们如何在命名空间之间传递匿名函数呢?

namespace MyNamespace {

class MyClass {
    function __construct(array $config) {
        $func = $config['func'];
        $value = 'Hello World';
        echo $func($value);   // This part throws the error
    }
}

}