【问题标题】:Which is the best way of multiple inheritance in php?php中多重继承的最佳方式是什么?
【发布时间】:2020-02-10 06:43:59
【问题描述】:

我有主类,应该扩展 3 个特定类。

主要要求是:

-对于 OOP,我需要在相互扩展的有意义的类中演示代码结构,因此将是主要的抽象类 产品逻辑。一种方法是拥有一个产品基础 类,它将包含所有产品共有的所有逻辑,然后 特定的产品类型将扩展产品基类 特定类型的东西。

-想法是创建一个包含所有产品通用逻辑的抽象类,如getTitle、setTitle等。然后创建子产品类 为每种产品类型存储产品类型特定的逻辑,例如 家具尺寸、CD 尺寸、书重等。

我已经用 php traits 解决了这个任务:

Main.php

class Main 
{
    use Book;
    use Disc;
    use Furniture;
    // common properties and methods
}

Disc.php

trait Disc
{
    public function setSize($size)
    {
        $this->size = $size;
    }  
}

Book.php

trait Book
{
    public function setWeight($weight)
    {
        $this->weight = $weight;
    }
}

Furniture.php

trait Furniture
{
    public function setHeight($height)
    {
        $this->height = $height;
    }
    public function setWidth($width)
    {
        $this->width = $width;
    }
    public function setLength($length)
    {
        $this->length = $length;
    }
}

我已经用这样的接口解决了这个问题:

Main.php

class Main
{
   // common properties
}

Types.php

interface Weight
{
    public function setWeight($weight);
}
interface Size
{
    public function setSize($size);
}
interface Fdim extends Weight,Size
{
    public function setHeight($height);
    public function setWidth($width);
    public function setLength($length);
}
class Furniture extends Main implements fdim
{
    public function setWeight($weight)
    {
        $this->weight = $weight;
    }
    public function setSize($size)
    {
        $this->size = $size;
    } 
    public function setHeight($height)
    {
        $this->height = $height;
    }
    public function setWidth($width)
    {
        $this->width = $width;
    }
    public function setLength($length)
    {
        $this->length = $length;
    }
}

这是满足上述要求的接口的最佳解决方案吗?

哪个特性或接口更容易接受?你有什么推荐?

我有一个问题:对于上述要求的任务,是否有更好的解决方案?我也尝试过抽象类和接口,但在我看来,需求对特征更满意。你有什么推荐的?

【问题讨论】:

  • 接口和特征将是要走的路。像 Java 这样的 PHP 只允许单继承,而 C++ 允许多继承但有问题。接口(和特征)可以帮助允许类似于多重继承的东西
  • 在我看来,最好将接口命名为HavingWeight,并且特征也更通用,例如WithWeightWithSize,这样你就不需要在@中使用别名了987654330@ 声明。然后使用Furniture 的抽象基类,Chair、Table 等将从其扩展。你在这里命名像具体类这样的特征,例如,Disc 应该从抽象 DataCarrier 扩展,DataCarrier 应该使用 WithSize 等。

标签: php class oop traits multiple-inheritance


【解决方案1】:

正如我在评论中所写,我会建议这样的事情:

abstract class Product
{
    // Get/Set title, SKU, properties which are common for *all* products
}

然后是一些接口:

interface HavingWeight
{
    // Get/Set weight
}

性状:

trait WithWeigth
{
    // Get/Set weight implementation
}

然后是Book的具体类:

class Book extends Product implements HavingWeight
{
   use WithWeight;
}

使用接口有一个非常重要的优势,那就是使用可以使用如下代码:

if($product instanceof HavingWeight)
{
    // Show weight
}

【讨论】:

  • #PeterM 拜托,你能帮我解决一下吗?什么应该把我放在身体里? if($product instanceof HavingWeight) { // Show weight }
  • 无论适合什么情况,这是使用产品实例的示例,您可以echo $product->getWeight() 并确定getWeight 方法实际上存在于当前实例中,如果$product
  • #PeterM 我是用这种方式做的,但是有错误..请看我的答案..
【解决方案2】:

正如Nikos M 提到的,PHP 不支持多重继承(至少到现在为止),并且您可能已经注意到,traitsinterfaces 将是模拟多重继承的唯一方法。

  • 如果多个类(不相互扩展或继承)需要使用相同的逻辑,请使用 traits
  • 您也可以使用 interfaces,但这意味着您不能在不同的类之间共享相同的函数逻辑(这将迫使您进行一些复制粘贴或考虑其他一些解决方法)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 2019-07-23
    • 2019-01-23
    相关资源
    最近更新 更多