} } class ProductFacade { private $products = array(); function __construct($file) { $this->file = $file; $this->compile(); } private function compile() { $lines = getProductFileLines($this->file); foreach ($lines as $line) { $id = getIDFromLine($line); $name = getNameFromLine($line); $this->products[$id] = getProductObjectFromID($id, $name); } } function getProducts() { return $this->products; } function getProduct($id) { return $this->products[$id]; } } $facade = new ProductFacade('test.txt'); $object = $facade->getProduct(234); print_r($object);
<?php include "noodles.php"; $facade = new ProductFacade(__DIR__ . "/test.txt"); print_r($facade->getProducts());
<?php class ProductFacade { private $products = array(); function __construct($file) { $this->file = $file; } private function compile() { $file = file_get_contents($this->file); /** * do estrutural stuffs * ex: read a file from csv */ } function getProducts() { return $this->products; } function getProduct($id) { return $this->products[$id]; } } $f = new ProductFacade('file.txt'); $f->getProduct(1);
$this->file = $file; $this->compile(); } public function getProductFileLines($file) { return file($file); } public function getProductObjectFromID($id, $productname) { return new Product($id, $productname); } public function getNameFromLine($line) { return 'test'; } public function getIDFromLine($line) { return '1'; } private function compile() { $lines = getProductFileLines($this->file); foreach ($lines as $line) { $id = getIDFromLine($line); $name = getNameFromLine($line); $this->products[$id] = geetProductObjectsFromID($id, $name); } } } $facade = new ProductFacade('test.txt'); $facade->getProduct(234);
{ private $products = array(); function __construct($file) { $this->file = $file; $this->compile(); } private function compile() { $lines = getProductFileLines($this->file); foreach ($lines as $line) { $id = getIDFromLine($line); $name = getNameFromLine($line); $this->products[$id] = getProductObjectFromID($id, $name); } } function getProducts() { return $this->products; } function getProduct($id) { if (isset($this->products[$id])) { return $this->products[$id]; } return null; } } $facade = new ProductFacade('facade.txt'); $object = $facade->getProduct(532); print_r($object);