在PHP中,如何在类中使用匿名函数?
如何在此类中调用在PHP中,如何在类中使用匿名函数?,php,anonymous-function,Php,Anonymous Function,如何在此类中调用$greet?我使用的是PHP5.5.4 <?PHP class Model { public $greet = function($name) { printf("Hello %s\r\n", $name); }; } $test = new Model(); $test->greet('World'); $test->greet('PHP'); ?> Parse error: syntax error,
$greet
?我使用的是PHP5.5.4
<?PHP
class Model
{
public $greet = function($name)
{
printf("Hello %s\r\n", $name);
};
}
$test = new Model();
$test->greet('World');
$test->greet('PHP');
?>
Parse error: syntax error, unexpected '$greet' (T_VARIABLE), expecting function (T_FUNCTION)
匿名函数在类之外工作正常(直接从类开始)
编辑:
现在我明白了
Fatal error: Call to undefined method Model::greet()
不能使用表达式的结果初始化对象变量。只允许使用静态/常量值。e、 g
class foo {
public $bar = 1+1; // illegal - cannot use an expression
public $baz = 2; // valid. '2' is a constant
}
您必须执行以下操作:
class foo {
public $bar;
function __construct() {
$this->bar = function() { .... }
}
}
并实际调用闭包,根据:
调用
greet
使PHP将其视为函数而不是属性。在PHP中,属性和方法的名称可以相同,因此区别是相关的
PHP7+
在PHP7中,调用绑定到属性的闭包不再需要\u call()
方法,因为。这将允许您在任何代码周围添加括号,就像在算术中一样
class Model
{
public $greet;
function __construct()
{
$this->greet = function($name)
{
printf("Hello %s\r\n", $name);
};
}
}
$test = new Model();
($test->greet)('johnny');
PHP 5
作为一种解决方法,您可以使用\u call()
magic方法。它将捕获对未定义的greet
方法的调用
class Model
{
public $greet;
function __construct()
{
$this->greet = function($name)
{
printf("Hello %s\r\n", $name);
};
}
function __call($method, $args)
{
if (isset($this->$method) && $this->$method instanceof \Closure) {
return call_user_func_array($this->$method, $args);
}
trigger_error("Call to undefined method " . get_called_class() . '::' . $method, E_USER_ERROR);
}
}
$test = new Model();
$test->greet('johnny');
我在课外怎么称呼它?我是否必须像任何旧变量一样,将其封装在另一个返回值的函数中?我尝试了各种方法,但都不起作用。如果在构造函数中构建闭包,
new
调用返回时,$bar
将“指向”闭包。所以$x=newfoo()$x->bar()代码>应该按预期工作。你能告诉我我在上面的编辑中做错了什么吗?编辑:对不起。我忘记编辑了!谢谢这似乎是一个很大的麻烦,所以也许我需要重新考虑使用它。例如,它在Javascript中看起来很不一样。。。。欢迎使用php。一致性无关紧要,一切都是虚构的。为什么要在构造函数或其他方法中分配函数?如果你不这样做(对我来说),它就不起作用了。你是说第一个例子中的解析错误
?在类定义中使用函数
关键字意味着创建一个方法,而不是一个匿名函数。不,我的意思是当您将匿名函数赋给变量时。它必须在函数(或构造函数)中完成。它不能只是一个成员变量。@johnny它是一个两阶段的东西。PHP代码首先(由)解释,然后执行。类定义(包括方法和属性)由解释器处理,而创建对象、匿名函数等都是在代码执行时完成的。@johnny在PHP7中,\u调用
方法将不再需要。PHP7现在在阿尔法。我已经相应地更新了答案。
class foo {
public $bar = 1+1; // illegal - cannot use an expression
public $baz = 2; // valid. '2' is a constant
}
class foo {
public $bar;
function __construct() {
$this->bar = function() { .... }
}
}
$x = new foo()
$x->bar->__invoke();
class Model
{
public $greet;
function __construct()
{
$this->greet = function($name)
{
printf("Hello %s\r\n", $name);
};
}
}
$test = new Model();
($test->greet)('johnny');
class Model
{
public $greet;
function __construct()
{
$this->greet = function($name)
{
printf("Hello %s\r\n", $name);
};
}
function __call($method, $args)
{
if (isset($this->$method) && $this->$method instanceof \Closure) {
return call_user_func_array($this->$method, $args);
}
trigger_error("Call to undefined method " . get_called_class() . '::' . $method, E_USER_ERROR);
}
}
$test = new Model();
$test->greet('johnny');