开发者

Interfaces and abstract classes in object oriented programming [duplicate]

This question already has answers here: Close开发者_运维百科d 12 years ago.

Possible Duplicate:

Interface vs Abstract Class (general OO)

Hi guys,

I decided to dig deep into OOP and have been taking on Design patterns practicing with PHP. I just had a question I would like views on. Interfaces and Abstract classes, which has more advantages? It might be vague but any light shed would be helpful. I can write code to use both and understand each one by concept but I am just wondering which should I be use by default, and why should I be using one over the other. Basically use-case scenarios.

Thanks in advance.

Eagletrophy


I use both. To me, interfaces are to describe functionality, and abstract classes are to provide base functionality.

What I mean, is whenever I require an "object type" as a parameter to a method/function, I almost always declare an interface for that purpose. So my methods have signatures like foo(BarInterface $bar)

Whenever I make multiple children from a class (and can share functionality), I make an abstract class that provides the base implementation (and also extends the interface)...

I hope that helps...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜