我对 Silex 中的这段代码有疑问。我正在编写一个小型网络购物车,但我不明白会话对象在 Silex 中是如何工作的。这是我的代码,它总是死于:
致命错误:函数名称必须是字符串会话
在这行中:
$amount = $app['calculate_amount']($app['session']->get('cart'));
$items = $app['calculate_items']($app['session']->get('cart'));
有任何想法吗?
谢谢!
干杯!
$app['calculate_amount'] = function ($cart) use ($app) {
$amount = 0.00;
$sql = "SELECT * FROM products WHERE id = ?";
if(is_array($cart))
{
foreach($cart as $id => $qty)
{
$product = $app['db']->fetchAssoc($sql, array($id));
$amount += $product["price"]*$qty;
}
}
return $amount;
};
$app['calculate_items'] = function ($cart) {
$items = 0;
if(is_array($cart))
{
foreach($cart as $id => $qty)
{
$items += $qty;
}
}
return $items;
};
$app->before(function (Request $request) use ($app) {
if ($request->get('save') != NULL)
{
foreach ($app['session']->get('cart') as $id_isbn => $qty)
{
if ($request->get('$id_isbn')=='0')
{
$app['session']->set('cart', array('$id_isbn' => NULL));
}
else
{
$app['session']->set('cart', array('$id_isbn' => $request->get('$id_isbn')));
}
}
$amount = $app['calculate_amount']($app['session']->get('cart'));
$items = $app['calculate_items']($app['session']->get('cart'));
$app['session']->set('amount', $amount);
$app['session']->set('items', $items);
}
});