php 中多重继承的最佳方式是什么?
Which is the best way of multiple inheritance in php?
我有主要的 class,应该扩展 3 个特定的 class。
主要要求是:
-For OOP I need to demonstrate code structuring in meaningful classes that extend each other, so would be an abstract class for the main
product logic. An approach for this would be to have a product base
class, that would hold all logic common to all products and then
specific product type would extend the product base class with that
type-specific stuff.
-The idea is to create an abstract class with all product common logic, like getTitle, setTitle etc. Then create child product classes
for each product type to store product type specific logic like
furniture sizes, CD size, book weight etc..
我用 php 特征解决了这个任务:
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;
}
}
这是满足上述要求的最佳接口解决方案吗?
哪个更容易被接受?你推荐什么?
我有一个问题:是否有更好的解决方案来完成上述要求的任务?我也尝试过抽象 classes 和接口,但我认为需求更满足于特征。你有什么建议?
如Nikos M所述,PHP不支持多重继承(至少目前不支持) ,并且您可能已经注意到,traits 和 interfaces 将是模拟多重继承的唯一方法。
- 如果多个 类(不相互扩展或继承)需要使用相同的逻辑,请使用 traits。
- 您也可以使用 interfaces,但这意味着您不能在不同的 类 之间共享相同的功能逻辑(这会迫使您进行一些复制-粘贴或考虑其他解决方法)。
正如我在评论中所写,我会建议这样的事情:
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:
class Book extends Product implements HavingWeight
{
use WithWeight;
}
使用接口有一个非常重要的优势,即可以使用如下代码:
if($product instanceof HavingWeight)
{
// Show weight
}
我有主要的 class,应该扩展 3 个特定的 class。
主要要求是:
-For OOP I need to demonstrate code structuring in meaningful classes that extend each other, so would be an abstract class for the main product logic. An approach for this would be to have a product base class, that would hold all logic common to all products and then specific product type would extend the product base class with that type-specific stuff.
-The idea is to create an abstract class with all product common logic, like getTitle, setTitle etc. Then create child product classes for each product type to store product type specific logic like furniture sizes, CD size, book weight etc..
我用 php 特征解决了这个任务:
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;
}
}
这是满足上述要求的最佳接口解决方案吗?
哪个更容易被接受?你推荐什么?
我有一个问题:是否有更好的解决方案来完成上述要求的任务?我也尝试过抽象 classes 和接口,但我认为需求更满足于特征。你有什么建议?
如Nikos M所述,PHP不支持多重继承(至少目前不支持) ,并且您可能已经注意到,traits 和 interfaces 将是模拟多重继承的唯一方法。
- 如果多个 类(不相互扩展或继承)需要使用相同的逻辑,请使用 traits。
- 您也可以使用 interfaces,但这意味着您不能在不同的 类 之间共享相同的功能逻辑(这会迫使您进行一些复制-粘贴或考虑其他解决方法)。
正如我在评论中所写,我会建议这样的事情:
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:
class Book extends Product implements HavingWeight
{
use WithWeight;
}
使用接口有一个非常重要的优势,即可以使用如下代码:
if($product instanceof HavingWeight)
{
// Show weight
}