【发布时间】:2012-10-11 12:32:30
【问题描述】:
在使用 Laravel 框架时,更具体地说 - 表单宏,我偶然发现了一个奇怪的错误。
一开始我以为是 Laravel 出了点问题,但后来我断章取意:
<?php
// placeholder function that takes variable as reference
$function = function(&$reference)
{
// append to variable
$reference = $reference . ':' . __METHOD__;
};
// test with straight call
$variable = 'something';
$function($variable);
echo $variable;
// test with call_user_func(), that gets called in Laravels case
$variable = 'something'; // reset
call_user_func($function, $variable);
echo $variable;
虽然对$function 的第一次调用正确执行,但对call_user_func() 的第二次尝试会产生(摘自键盘):
Warning: Parameter 1 to {closure}() expected to be a reference, value given
PHP Warning: Parameter 1 to {closure}() expected to be a reference, value given
在写这篇文章的时候,我想到了call_user_func_array():fiddle here,但是产生了同样的错误。
我的引用有什么问题吗?或者这是 PHP 的错误?
【问题讨论】:
标签: php function reference anonymous-function