我正在检查一些PHP 5.3.0功能,并在网站上遇到一些看起来很有趣的代码:
public function getTotal($tax)
{
$total = 0.00;
$callback =
/* This line here: */
function ($quantity, $product) use ($tax, &$total)
{
$pricePerItem = constant(__CLASS__ . "::PRICE_" .
strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback);
return round($total, 2);
}
Run Code Online (Sandbox Code Playgroud)
作为匿名函数的一个例子.
有人知道吗?有文件吗?如果它被使用它看起来很邪恶?
恐怕我有一个 PHP 初学者问题。我正在使用 Symfony 的缓存组件。https://symfony.com/doc/current/components/cache.html
我在接收 2 个参数($url、$params)的函数内部调用缓存对象。
class MainController extends AbstractController {
public function do($url, $params) {
$cache = new FilesystemAdapter();
return $cache->get('myCacheName', function (ItemInterface $c) {
global $url;
var_dump($url); // ---> null !!!!
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,我无法访问缓存方法调用内部的函数参数。$url 和 $params 为空。当然,我可以在 MainController 类中使用公共类变量来来回发送变量,但这似乎有点笨拙。