The abstract class is Product and the abstract class should be extended to 3 different product classes. I have created the Product class with Model, Controller and View class. Now I have to make the Product class into an Abstract class. Can you explain the file structure. The below code is working fine. I have been asked to convert the product class into an abstract class and that abstract class should contain 3 different child classes eg: Book, TV, bulb. My question is it possible to work with abstract classes using MVC pattern. and if possible to which classes I should create the controller and View.
Product class
class Product extends DBConn
{
protected function getProducts()
{
$sql = "SELECT * FROM product";
$query = $this->connect()->prepare($sql);
$query->execute();
return $query;
}
protected function getRowCount()
{
$sql = "SELECT * FROM product";
$query = $this->connect()->prepare($sql);
$query->execute();
$rowCount = $query->rowCount();
return $rowCount;
}
protected function setProducts($sku, $name, $price, $productType)
{
$sql = "INSERT INTO product (SKU, productName, price, productType) VALUES (?,?,?,?) ";
$query = $this->connect()->prepare($sql);
$query->execute([$sku, $name, $price, $productType]);
return $query;
}
protected function deleteProducts($sku)
{
$placeholder = implode(',', array_fill(0, count($sku), '?'));
$sql = "DELETE FROM product WHERE SKU IN ($placeholder)";
$query = $this->connect()->prepare($sql);
$query->execute($sku);
}
}
Product Controller
class ProductController extends Product
{
public function createProduct($sku, $productName, $price, $productType)
{
session_start();
try {
$this->setProducts($sku, $productName, $price, $productType);
}
catch (Exception $e) {
if ($e == true) {
$_SESSION['add-btn'] = "NO PRODUCTS ADDED! Error: " . $e->getMessage();
header("Location:../addProducts.php");
die();
}
}
finally {
$_SESSION['add-btn'] = "Products added succesfully";
header("Location:../addProducts.php");
}
}
public function massDelete($sku)
{
session_start();
$this->deleteProducts($sku);
try {
$this->deleteProducts($sku);
}
catch (Exception $e) {
if ($e == true) {
$_SESSION['status'] = "NO PRODUCTS DELETED! Error: " . $e->getMessage();
header("Location:../index.php");
die();
}
}
finally {
$_SESSION['status'] = "Products deleted succesfully";
header("Location:../index.php");
}
}
}
Product View
class ProductView extends Product
{
public function displayProductDetails()
{
$query = $this->getProducts();
return $query;
}
public function showRowCount()
{
$rowCount = $this->getRowCount();
return $rowCount;
}
}