public static function getInstance($id, PDO $pdo) { $query = "select * from products where id='{$id}'"; $stmt = $pdo->query($query); $row = $stmt->fetch(); if (empty($row)) { return null; } if ($row['type'] == "book") { $product = new BookProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['numpages']); } if ($row['type'] == "cd") { $product = new CdProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['playlength']); } else { $product = new ShopProduct($row['title'], $row['firstname'], $row['mainname'], $row['price']); } $product->setID($row['id']); $product->setDiscount($row['discount']); return $product; }
public static function getInstance($id, PDO $pdo) { $stmt = $pdo->prepare("select * from products WHERE id=?"); $result = $stmt->execute(array($id)); $row = $stmt->fetch(); if (empty($row)) { return null; } if ($row['type'] == "book") { $product = new BookProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['numpages']); } else { if ($row['type'] == "cd") { $product = new CDProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['playlength']); } else { $product = new ShopProduct($row['title'], $row['firstname'], $row['mainname'], $row['price']); } } $product->setID($row['id']); $product->setDiscount($row['discount']); return $product; }