【发布时间】:2013-07-12 19:06:03
【问题描述】:
在 PHP 中有一个名为 extract 的函数,它接受一个数组,并将您的数据转换为 PHP 变量。当我需要将变量发送到包含时,此功能非常有用。例如。
extract(array( "test" => 123 ));
require "test.php"
所以 test.php:print($test);
返回:123
我需要对函数做同样的事情(我可能不知道)。 PHP 5.4 支持use (Anonymous Function),这很有趣。例如。
$test = 123;
call_user_func(function() use($test) {
print($test);
});
但是,我需要传递具有其他名称和数量的变量。 类似:
$useArgs = array( "a" => 1, "b" => 2, "c" => 3 );
call_user_func(function() use(extract($useArgs)) {
print($a);
print($b);
print($c);
if(isset($d)) {
print($d);
}
});
这怎么可能?
【问题讨论】: