我希望能够在类的构造函数中动态创建实例方法,如下所示:
class Foo{
function __construct() {
$code = 'print hi;';
$sayHi = create_function( '', $code);
print "$sayHi"; //prints lambda_2
print $sayHi(); // prints 'hi'
$this->sayHi = $sayHi;
}
}
$f = new Foo;
$f->sayHi(); //Fatal error: Call to undefined method Foo::sayHi() in /export/home/web/private/htdocs/staff/cohenaa/dev-drupal-2/sites/all/modules/devel/devel.module(1086) : eval()'d code on line 12
Run Code Online (Sandbox Code Playgroud)
问题似乎是lambda_2函数对象没有在构造函数中绑定到$ this.
任何帮助表示赞赏.
Gor*_*don 18
您将匿名函数分配给属性,但然后尝试使用属性名称调用方法.PHP无法从属性中自动取消引用匿名函数.以下将有效
class Foo{
function __construct() {
$this->sayHi = create_function( '', 'print "hi";');
}
}
$foo = new Foo;
$fn = $foo->sayHi;
$fn(); // hi
Run Code Online (Sandbox Code Playgroud)
您可以利用魔术__call方法拦截无效方法调用,以查看是否存在包含回调/匿名函数的属性,但:
class Foo{
public function __construct()
{
$this->sayHi = create_function( '', 'print "hi";');
}
public function __call($method, $args)
{
if(property_exists($this, $method)) {
if(is_callable($this->$method)) {
return call_user_func_array($this->$method, $args);
}
}
}
}
$foo = new Foo;
$foo->sayHi(); // hi
Run Code Online (Sandbox Code Playgroud)
从PHP5.3开始,您还可以创建Lambdas
$lambda = function() { return TRUE; };
Run Code Online (Sandbox Code Playgroud)