У меня есть родительский абстрактный класс - Main.php, который расширен на 3 дочерних класса.
Основное требование:
Создать абстрактный класс со всей логикой продуктакак getTitle, setTitle и т. д. Затем создайте дочерние классы продуктов для каждого типа продукта, чтобы хранить логику, специфичную для типа продукта, такую как размеры мебели, размер компакт-диска, вес книги.
Проблема лежитв следующем:
Когда я создаю экземпляр объекта, я получаю данные только для одного продукта (см. изображение ..):
Как я могу решить эту проблему?
Main.php
<?php
include "classes/DB.php";
abstract class Main
{
public $table = 'products';
private $barcode;
private $name;
private $price;
private $image;
protected $size;
protected $height;
protected $width;
protected $length;
protected $weight;
// SET Parametres
public function setBarcode($barcode)
{
$this->barcode = $barcode;
}
public function setName($name)
{
$this->name = $name;
}
public function setPrice($price)
{
$this->price = $price;
}
public function setImage($image)
{
$this->image = $image;
}
abstract function setSize($size);
abstract function setHeight($height);
abstract function setWidth($width);
abstract function setLength($length);
abstract function setWeight($weight);
// Create Data
public function insert()
{
$sql = "INSERT INTO $this->table(barcode, name, price, size, height, width, length, weight, image)VALUES(:barcode, :name, :price, :size, :height, :width, :length, :weight, :image)";
$stmt = DB::prepare($sql);
$stmt->bindParam(':barcode', $this->barcode);
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':price', $this->price);
$stmt->bindParam(':size', $this->size);
$stmt->bindParam(':height', $this->height);
$stmt->bindParam(':width', $this->width);
$stmt->bindParam(':length', $this->length);
$stmt->bindParam(':weight', $this->weight);
$stmt->bindParam(':image', $this->image);
return $stmt->execute();
}
// Read Data
public function readAll()
{
$sql = "SELECT * FROM $this->table";
$stmt = DB::prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
// Delete Data
public function delete(array $id)
{
$placeholders = trim(str_repeat('?,', count($id)), ',');
$sql = "DELETE FROM $this->table WHERE id IN ($placeholders)";
$stmt = DB::prepare($sql);
return $stmt->execute($id);
}
}
?>
Book.php
<?php
include "classes/Main.php";
class Book extends Main
{
// SET Parametre
public function setWeight($weight)
{
$this->weight = $weight;
}
public function setSize($size){}
public function setHeight($height){}
public function setWidth($width){}
public function setLength($length){}
}
?>
Disk.php
<?php
include "classes/Main.php";
class Disk extends Main
{
// SET Parametre
public function setSize($size)
{
$this->size = $size;
}
public function setWeight($weight){}
public function setHeight($height){}
public function setWidth($width){}
public function setLength($length){}
}
?>
Furniture.php
<?php
include "classes/Main.php";
class Furniture extends Main
{
// SET Parametre
public function setHeight($height)
{
$this->height = $height;
}
public function setWidth($width)
{
$this->width = $width;
}
public function setLength($length)
{
$this->length = $length;
}
public function setWeight($weight) {}
public function setSize($size) {}
}
?>
index.php
/ PHP AutoLoader
spl_autoload_register(function($class_name) {
include "classes/".$class_name.".php";
});
$user = new DisK();
if (isset($_POST['create'])) {
$barcode = 'SKU'.uniqid($_POST['barcode']);
$name = $_POST['name'];
$price = $_POST['price'];
// fixing Undefined Index error
$size = isset($_POST['size']) ? $_POST['size'] : '';
$height = isset($_POST['height']) ? $_POST['height'] : '';
$width = isset($_POST['width']) ? $_POST['width'] : '';
$length = isset($_POST['length']) ? $_POST['length'] : '';
$weight = isset($_POST['weight']) ? $_POST['weight'] : '';
$image = $_POST['image'];
$user->setBarcode($barcode);
$user->setName($name);
$user->setPrice($price);
$user->setSize($size);
$user->setHeight($height);
$user->setWidth($width);
$user->setLength($length);
$user->setWeight($weight);
$user->setImage($image);
}
// Delede Data
if (isset($_POST['delete'])) {
$id = $_POST['id'];
if ($user->delete($id)) {
echo "<div class='alert alert-info col-md-4 text-danger ml-5 mt-3' role='alert'><strong> Product Deleted Successfully </strong></div>";
}
}
?>