Skip to content

Instantly share code, notes, and snippets.

@clarkf
Created March 21, 2014 02:41
Show Gist options
  • Save clarkf/9678425 to your computer and use it in GitHub Desktop.
Save clarkf/9678425 to your computer and use it in GitHub Desktop.
A really bad Dependency Injection example
<?php
namespace Acme\CookieRobot\Controllers;
use Acme\FoodInventory;
class EggProcurementController extends BaseController
{
/**
* @type Acme\FoodInventory
*/
protected $inventory;
/**
* Construct a new EggProcurementController
*
* @param Acme\FoodInventory $inventory
*/
public function __construct(FoodInventory $inventory)
{
$this->inventory = $inventory;
}
/**
* Procure some amount of eggs
*
* @param int $quantity The number of eggs to procure
*
* @return Acme\Egg[] An array of eggs
*/
public function procure($quantity = 2)
{
// Silly pseudo-code follows...
if (!$this->inventory->has(FoodInventory::EGG, $quantity)) {
throw new InvalidArgumentException(
"Unable to locate $quantity eggs!"
);
}
$this->moveTo($this->inventory->locationOf(FoodInventory::EGG));
$eggs = $this->acquire(FoodInventory::EGG, $quantity);
$this->moveBack();
return $eggs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment