Can someone explain me, how to include abstract class in PHP MVC

Viewed 49

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;
    }

}
1 Answers

Its very hard to explain it in details.. But let me try.

Generally we use abstract class or interface to create a prototype of a class. When I say prototype, it means the interface or the abstract class will have the common methods "declared" or "defined" and then on the child classes (where we implement the interface or inherit the abstract class) we implement / override the methods.

In your case the product class has no abstract method. All the methods are already defined in that class. To call a class an "Abstract" class, you must have at least one abstract method (which will be just the declaration).

Now the question is When and Why do we use Abstract Class or Interface. The answer is, when we know that a certain class will must have some pre-defined functions, but the function definitions can very in child classes due to different nature / behavior of different child classes.

Let's say, we have an interface called Car, we can define it like ..

<?php

interface Car
{
    public function accelerate($throttlePosition);

    public function break($breakingForce);
}

Now, this becomes an interface as bot the methods are just declared and their body is not defined. Now lets say I have 2 different classes for 2 different type of cars..

<?php
    
    class Bmw implements Car
    {
        public function accelerate($throttlePosition){
           // Here we write the logic of how the BMW 
           // engine handles the acceleration. 
           // As its a different engine, the acceleration algorithm must be 
           // different than Mercedes
        }
    
        public function break($breakingForce){
           // The same way we define the breaking logic of BMW,
           // This will again not be same as Mercedes
        }

        public function specialFunctionA(){
           // This is available in BMW only
        }
    }


    
    class Mercedes implements Car
    {
        public function accelerate($throttlePosition){
           // Define acceleration algorithm of Mercedes
        }
    
        public function break($breakingForce){
           // Define breaking algorithm of Mercedes
        }

        public function specialFunctionB(){
           // This is available in Mercedes only
        }

        public function specialFunctionC(){
           // This is available in Mercedes only
        }
    }

Now I created the Car as an "Interface", because I want all my Cars (BMW, Mercedes) to have those functions "Implemented" / "Defined", because without those function the car won't work. The individual Bmw and Mercedes classes may have their own sets of functions as well (specialFunctionA, specialFunctionB, specialFunctionC) (Obviously these 2 cars will have some unique features functions, that are not common).

Now let's say you instruct your developers that you must implement the "Car" interface for every different type of car you are developing. Then they must implement those 2 function in their classes and afterwards in your program, where ever you create an object of any of these car classes (may it be Bmw or Mercedes), you know that they must be having those 2 functions and you can call those functions without worrying about if those functions exists or not.

This is where the "Abstraction" thing comes in.

Now I am not sure why you will need your Product class to be an Abstract Class or an Interface, because I don't think that different product will have different logics / definitions for those functions (getProducts, getRowCounts, setProducts).

Now if you really think that you need an abstraction where, try to understand "why" and "when" we use abstraction.

Hope it will help you.

Related