Как я могу расположить несколько классов с абстракцией?
Итак, будет один базовый абстрактный класс, а остальные три класса расширят базовый абстрактный класс.
Идея состоит в том, чтобысоздайте абстрактный класс со всей общей логикой продукта, такой как getTitle, setTitle и т. д. Затем создайте дочерние классы продукта для каждого типа продукта, чтобы хранить логику, специфичную для типа продукта, такую как размеры мебели, размер CD, вес книги и т. д.
Базовый класс - Main.php
<?php
include "classes/DB.php";
abstract class Main
{
protected $table;
private $barcode;
private $name;
private $price;
protected $size;
protected $height;
protected $width;
protected $length;
protected $weight;
private $image;
// SET Parametres
public function setBarcode($barcode)
{
$this->barcode = $barcode;
}
public function setName($name)
{
$this->name = $name;
}
public function setPrice($price)
{
$this->price = $price;
}
abstract function setSize($size);
abstract function setHeight($height);
abstract function setWidth($width);
abstract function setLength($length);
abstract function setWeight($weight);
public function setImage($image)
{
$this->image = $image;
}
// 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);
}
}
?>
Disk.php
<?php
include "classes/Main.php";
class Disk extends Book
{
protected $table = 'products';
protected $size;
// SET Parametre
public function setSize($size)
{
$this->size = $size;
}
}
?>
Book.php
<?php
include "classes/Main.php";
class Book extends Main
{
protected $table = 'products';
protected $weight;
// SET Parametre
public function setWeight($weight)
{
$this->weight = $weight;
}
}
?>
Furniture.php
<?php
include "classes/Main.php";
class Furniture extends Disc
{
protected $table = 'products';
protected $height;
protected $width;
protected $length;
// SET Parametre
public function setHeight($height)
{
$this->height = $height;
}
public function setWidth($width)
{
$this->width = $width;
}
public function setLength($length)
{
$this->length = $length;
}
}
?>