class Book{
public $id;
public $name;
public function __construct($id, $name){
$this->id = $id;
$this->name = $name;
}
}
class BookCollection{
protected $db;
public $data;
public function __construct(PDO $db){
$this->db = $db;
}
private function get($id = NULL){
if(empty($id)){
$stmt = $this->db->query("SELECT `id`, `name` FROM `book` ORDER BY `id`");
}else{
$stmt = $this->db->prepare("SELECT `id`, `name` FROM `book` WHERE `id` = ?");
$stmt->execute([$id]);
}
foreach($stmt->fetchAll() as $v){
$this->data[] = new Book($v['id'], $v['name']);
}
return $this->data;
}
public function getById($id){
return $this->get($id);
}
public function getAll(){
return $this->get();
}
public function save(BOOK $book){
$stmt = $this->db->prepare("INSERT INTO `book` (`name`) VALUE (?)");
return $stmt->execute([$book->name]);
}
}
$book_repository = new BookCollection($db);
$result = $book_repository->getAll();
var_dump($result);
$result = $book_repository->getById(1);
var_dump($result);
$add = new Book('', 'BookNew');
$book_repository->save($add);
bookGetAll();
bookGetById(1);
bookCreate($name, price);
bookUpdate($id, name, price);
в каком формате работать с книгами в коде - в виде массивов или объектов, а не как ими управлять.