id, title, price, slugの4つのフィールドを持つItemクラスがあります。現在自分が扱っている商品は20個足らずで、今後増えても30個はまず超えません。
このような少数のデータであっても、データベースに保存すべきでしょうか?
現在は以下のようにクラスに定義しています。
php
1<?php 2 3class Item 4{ 5 private $id; 6 private $title; 7 private $price; 8 private $slug; 9 10 public function __construct($id, $title, $price, $slug) 11 { 12 $this->id = $id; 13 $this->title = $title; 14 $this->price = $price; 15 $this->slug = $slug; 16 } 17 18 // 以下getter省略 19} 20 21class ItemRepository 22{ 23 private $items; 24 25 // id 26 const ITEM_A = 1; 27 const ITEM_B = 2; 28 const ITEM_C = 3; 29 // 以下略 30 31 private static $itemList = [ 32 self::ITEM_A => [ 33 'title' => 'hoge', 34 'price' => 500, 35 'slug' => 'fuga', 36 ], 37 // 以下略 38 ]; 39 40 public function __construct() 41 { 42 foreach (self::$itemList as $id => $item) { 43 $this->items[$id] = new Item( 44 $item['id'], $item['title'], $item['price'], $item['slug'] 45 ); 46 } 47 } 48 49 public function find($id) 50 { 51 if (isset($this->items[$id])) { 52 return $this->items[$id]; 53 } 54 throw new \InvalidArgumentException('不正なID'); 55 } 56 57 public function findBySlug($slug) {//省略} 58}
また、あわせてお聞きしたいのですが、ItemRepositoryクラスについてもっといい書き方は無いでしょうか?
コンストラクタでItemのインスタンスをセットしてるのがちょっとイケてない気がしています。
よろしくおねがいします。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/04 01:59