【发布时间】:2012-06-27 23:47:48
【问题描述】:
假设Human 的口袋里可以有Item(s)。每个Item对Human有不同的影响。
当他使用一个物品时,它会转到ItemController:
class ItemController extends Controller
{
public function useAction() {
// Human
$human = new Human();
// Item
$request = Request::createFromGlobals();
$item_id = $request->request->get('item_id', 0);
$item = new Item($item_id);
// Different effects depending on the Item used
switch($item->getArticleId()) {
case 1: $this->heal($human, $item, 5); break; // small potion
case 2: $this->heal($human, $item, 10); break; // medium potion
case 3: $this->heal($human, $item, 15); break; // big potion
}
}
// The following Heal Function can be placed here ?
private function heal($human, $item, $life_points) {
$human->addLife($life_points);
$item->remove();
return new Response("You have been healed of $life_points");
}
}
heal function 可以放在这里吗?我认为它不应该在 Controller 中。但我也认为它不应该放在Item Entity 内(因为响应,并且因为它使用$Human)
【问题讨论】:
-
您上面的代码是否只是可能发生的事情的一个示例,而不是您使用的实际代码?
标签: php symfony controller