如何使用instanceof运算符实例化抽象类?

问题描述 投票:0回答:1

我有主班,应该再增加3个特定班。为此,我使用主parent product abstract class,,它结合了所有常见的逻辑,例如getTitle,setTitle等。然后每个产品类型都有3个子产品类,用于存储特定于产品类型的逻辑,例如家具尺寸, CD尺寸,书本重量等。

对于多重继承,我使用抽象类,特征和类之间的接口。

所以我有2个文件:

Product.php-用于所有常见逻辑:

abstract class Product
{
    public $table = 'products';
    public $barcode;
    public $name;
    public $price;
    public $image;

    protected $height;
    protected $width;
    protected $length;
    protected $size;
    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;
    }

    // Read Data
    public function readAll()
    {
        $sql = "SELECT * FROM $this->table";
        $stmt = DB::prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    // 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();
    }

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

Types.php-三种特殊类型:

// interfaces of each product type
interface HavingWeight
{
    public function setWeight($weight);
}

interface HavingSize
{
    public function setSize($size);
}

interface HavingFur_dims
{
    public function setHeight($height);
    public function setWidth($width);
    public function setLength($length);
}

// traits of each product type
trait WithWeight
{
    // setters
    public function setWeight($weight)
    {
        $this->weight = $weight;
    }
}

trait WithSize
{
    // setters    
    public function setSize($size)
    {
        $this->size = $size;
    } 
}

trait WithFur_dims
{
    // setters
    public function setHeight($height)
    {
        $this->height = $height;
    }

    public function setWidth($width)
    {
        $this->width = $width;
    }

    public function setLength($length)
    {
        $this->length = $length;
    }
}

// Child classes
class Book extends Product implements HavingWeight
{
   use WithWeight;
}

class Disc extends Product implements HavingSize
{
   use WithSize;
}

class Furniture extends Product implements HavingFur_dims
{
   use WithFur_dims;
}

最后,考虑所有这些,我有以下问题:

我如何实例化所有这些并使用instanceof运算符?

请,您能给我看看具体的例子吗?

php oop abstract-class multiple-inheritance instanceof
1个回答
0
投票

那只是一个客人,但也许您需要这样的东西:

$book = new Book();
$book->table = 'books';
...
$book->setWeight(2);
....
var_dump($book->readAll());

很难知道您的期望,并且代码中还有其他“错误”之处,因此请您解释更多。

© www.soinside.com 2019 - 2024. All rights reserved.