Skip to content

Instantly share code, notes, and snippets.

@rightgo09
Created January 18, 2015 14:32
Show Gist options
  • Select an option

  • Save rightgo09/972f76b5654ebebeea01 to your computer and use it in GitHub Desktop.

Select an option

Save rightgo09/972f76b5654ebebeea01 to your computer and use it in GitHub Desktop.
<?php
require_once "./simpleitemfactory.php";
class ItemShop {
private $factory;
public function __construct(SimpleItemFactory $factory) {
$this->factory = $factory;
}
public function order_item($type) {
$item = $this->factory->create_item($type);
return $item;
}
}
<?php
require_once "./simpleitemfactory.php";
require_once "./itemshop.php";
$item = (new ItemShop(new SimpleItemFactory()))->order_item("sword");
var_dump($item);
//=>object(SwordItem)#3 (0) {
//=>}
<?php
class ShoesItem {
}
<?php
require_once "./sworditem.php";
require_once "./shoesitem.php";
class SimpleItemFactory {
public function create_item($type) {
$item = null;
if ($type === "sword") {
$item = new SwordItem();
}
elseif ($type === "shoes") {
$item = new ShoesItem();
}
return $item;
}
}
<?php
class SwordItem {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment