如何在php中使用抽象类实现多态性以获取、保存和删除三种不同的产品类型

waxmsbnn  于 2023-01-12  发布在  PHP
关注(0)|答案(1)|浏览(102)

如何使用抽象类实现多态性,用于处理三种不同产品类型的产品保存、删除和显示?有三种产品类型DVD家具书籍
以下代码是我的产品型号

class Product
{
private $db;

public function __construct()
{
    $this->db = new Database;
}

public function getProducts()
{
    $this->db->query("SELECT * FROM products ORDER BY ID ASC");
    return $this->db->__get('resultSet');
}

public function findProductsBysku($data)
{

    $this->db->query('SELECT * FROM products WHERE sku = :sku');
    //Bind values
    $this->db->bind(':sku', $data['sku']);

    //get products
    return $this->db->__get('resultSet');

}

public function insertProducts($data)
{

    $this->db->query('INSERT INTO products (sku, name, price, size, height, width, length, weight) VALUES (:sku, :name, :price, :size, :height, :width, :length, :weight)');
    // Bind values
    $this->db->__set(':sku', $data['sku']);
    $this->db->__set(':name', $data['name']);
    $this->db->__set(':price', $data['price']);
    $this->db->__set(':size', $data['size']);
    $this->db->__set(':height', $data['height']);
    $this->db->__set(':width', $data['width']);
    $this->db->__set(':length', $data['length']);
    $this->db->__set(':weight', $data['weight']);
    // execute
    if ($this->db->execute()) {
        $response = array("message" => "The product added", "ResultStatus" => 200);
        return json_encode($response);
    }
}

public function deleteProduct($id)
{
    $this->db->query('DELETE FROM products WHERE id = :id');
    // Bind values
    $this->db->bind(':id', $id);
    // Execute
    if ($this->db->execute()) {
        return true;
    } else {
        return false;
    }
  }
}

我在名为AddProductProducts的两个控制器中使用它:

添加产品

class Addproduct extends Controller
{
public $productModel;
public function __construct()
{

    $this->productModel = $this->model('Product');

}

public function index()
{
    /* Allow cors */
    header('Access-Control-Allow-Origin: *');

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {

        $data = [
            'sku' => $_POST['sku'],
            'name' => $_POST['name'],
            'price' => $_POST['price'],
            'size' => $_POST['size'],
            'height' => $_POST['height'],
            'width' => $_POST['width'],
            'length' => $_POST['length'],
            'weight' => $_POST['weight']
        ];

        /* Find product by sku */
        $productsBysku = $this->productModel->findProductsBysku($data);
        /* Check if product already exist */
        if (count($productsBysku) > 0) {
            $response = array("message" => "The product already exist", "ResultStatus" => 500);
            echo json_encode($response);
        } else {
            /* insert product */
            $res = $this->productModel->insertProducts($data);
            echo $res;
        }

    }

  }
 }

产品:

class Products extends Controller
 {
  public $productModel;
  public function __construct()
{
    $this->productModel = $this->model('Product');
}

public function index($id)
{
    /* Allow cors */
    header('Access-Control-Allow-Origin: *');

    //load products
    $products = $this->productModel->getProducts();
    $this->view('pages/index', ['Products' => $products]);

}

public function MassDelete() {
    /* Allow cors */
    header('Access-Control-Allow-Origin: *');

    //handle mass delete request
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $ids = $_POST['id'];
        foreach ($ids as $id) {
            $this->productModel->deleteProduct($id);
        }
    }

    //load products
    $products = $this->productModel->getProducts();
    $this->view('pages/index', ['Products' => $products]);
    
   }
 }

我必须将产品型号按类型分解为不同的类,产品型号是它们的基类,另外一个重要注意事项是我不应使用任何条件语句来处理产品类型

w9apscun

w9apscun1#

只需创建一个抽象类,并从它扩展。然后在你的控制器类中,简单地操作该类,而不是它的子类。

abstract class Product {
    public function __construct(
        private string $sku,
        private string $name
    ) {}

    public function getSku(): string
    {
        return $this->sku;
    }
}

class DVDProduct extends Product {
    public function __construct(
        private string $sku,
        private string $name,
        private float $rottenTomatosRating
    ) {
        parent::__construct($sku, $name);
    }

    public function getRottenTomatosRating(): float
    {
        return $this->rottenTomatosRating;
    }
}

相关问题