PHP OOP结构问题,模拟多重继承 [英] PHP OOP structure problem, simulate multiple inheritance

查看:79
本文介绍了PHP OOP结构问题,模拟多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一家拥有多种产品类型的电子商店.我会想到以下结构

I have an e-shop with multiple product types. And i would have thought of the following structure

Cart_Item
-- Cart_Product
-- Cart_Download

Order_Item extends Cart_Item
-- Order_Product
-- Order_Download

问题是我想让Order_Product扩展Order_Item和Cart_Product. 这是因为它需要通用的Order_Item方法(从产品而不是从Order获取价格),还需要Cart_Product的方法(运输计算)

The problem is that i want to have Order_Product extend Order_Item and Cart_Product. This is because it needs method generic to Order_Item ( get price from Order not from product ) but also methods from Cart_Product ( shipping calculations )

我知道php不支持多重继承,我在徘徊什么是最干净的方法来模仿它.

I know that php doesn't support multiple inheritance, i was wandering what is the cleanest way to emulate this.

现在我已经从Order_Product中的Order_Item中扩展了Order_Product的Cart_Product重复代码,并进行了Order_Download.

Right now i have Order_Product extend Cart_Product duplicate code from Order_Item in Order_Product an Order_Download.

推荐答案

要么使用策略实施方法.或使用组成而不是继承,这意味着您让Order_Product具有一个Order_Item和一个Cart_Product.

Either use Interfaces and implement the methods manually or via Strategies. Or use Composition instead of Inheritance, meaning you let the Order_Product have a Order_Item and a Cart_Product.

在旁注中:您还可以考虑对自己的Service类进行运输计算",然后将适当的Product实例传递给该Service类.

这篇关于PHP OOP结构问题,模拟多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆