The following code causes PHP to throw an error:
namespace NamespaceOne;
class MyClass {
function __construct( array $config ) {
$func = $config['func'];
$value = 'Hello World';
echo $func( $value ); // This part throws the error
}
}
The class is instantiated in a different file:
namespace NamespaceTwo;
$class = new \NamespaceOne\MyClass( array(
'func' => function( $v ) { return $v; }
));
Terminates with the error:
Fatal error: Function name must be a string [...]
EDIT
If I re-declare the function inside the namespace, it works:
class MyClass {
function __construct( array $config ) {
$config['func'] = function( $v ) { return $v; };
$func = $config['func'];
$value = 'Hello World';
echo $func( $value ); // Echos "Hello World"
}
}
So now we know what causes it to break, but how do we pass an anonymous function between namespaces?
namespace MyNamespace {
class MyClass {
function __construct(array $config) {
$func = $config['func'];
$value = 'Hello World';
echo $func($value); // This part throws the error
}
}
}