<?php
require_once 'Item.php';
class ItemFactory
{
private $_pool;
private static $_instance = null;
private function __construct($filename)
{
$this->buildPool($filename);
}
public static function getInstance($filename)
{
if (is_null(self::$_instance)) {
self::$_instance = new ItemFactory($filename);
}
return self::$_instance;
}
public function getItem($id)
{
if (array_key_exists($id, $this->_pool)) {
return $this->_pool[$id];
} else {
return null;
}
}
private function buildPool($filename)
{
$fp = fopen('item_data.csv', 'r');
$dummy = fgets($fp, 1024);
$this->_pool = array();
while ($buffer = fgets($fp, 1024)) {
list($itemId, $itemName, $itemPrice) = explode(',', rtrim($buffer));
$this->_pool[$itemId] = new Item($itemId, $itemName, $itemPrice);
}
fclose($fp);
}
public final function __clone()
{
throw new LogicException('Clone is not allowed against [' . get_class($this) . '].');
}
}