简体   繁体   English

php:何时使用抽象和接口类?

[英]php: When to use abstract and interface class?

I was wondering how you guys decide when to use abstract or interface class during the application development since they both provide similar functionalities with slightly difference. 我想知道你们如何在应用程序开发期间决定何时使用抽象类或接口类,因为它们都提供了类似的功能,但略有不同。 I appreciate any helps. 我感谢任何帮助。 Thanks. 谢谢。

Use abstraction if you have default methods (with accompanying code) for inheritors. 如果您有继承者的默认方法(附带代码),请使用抽象。 Use interfaces if you just need to make sure that classes inheriting from this parent should implement all methods defined. 如果您只需确保从此父级继承的类应实现定义的所有方法,请使用接口。 Word of caution: use abstract classes and interfaces only if you intend to enforce structure and organization (usually with a team). 注意事项:只有在您打算强制执行结构和组织(通常是团队)时才使用抽象类和接口。 There's a performance overhead. 有性能开销。

I use abstract classes when I want the inheriting classes to inherit some functionality, and interfaces when I want to set some minimum structural criteria for a group of classes. 当我希望继承类继承某些功能时,我使用抽象类,当我想为一组类设置一些最小结构标准时,我使用接口。

One thing to remember is that any given class can "inherit" (technically implement) many interfaces but only one sub-class (be that abstract or not). 要记住的一件事是,任何给定的类都可以“继承”(技术上实现)许多接口但只有一个子类(不管是抽象的还是非抽象的)。

when i'm developing and trying to decide whether to use an interface or an abstract class i usually think about whether classes that will inherit will contain only the same structure (methods, properties etc) but different implementations. 当我正在开发并试图决定是使用接口还是抽象类时,我通常会考虑将继承的类是否只包含相同的结构(方法,属性等)但是实现不同。

if the implementation of the methods will be different, but i want to ensure sameness from a structural standpoint i use an interface. 如果方法的实现会有所不同,但我想从结构的角度确保相同性,我使用的是接口。 if the structure and implementation are the same i tend to use abstract classes. 如果结构和实现是相同的,我倾向于使用抽象类。

Languages like PHP support multiple interface implementation but not multiple class inheritance, and this is often part of the decision -- will your class support multiple behaviors (interfaces) or will it act as one type of thing (base class)? 像PHP这样的语言支持多个接口实现,但不支持多类继承,这通常是决定的一部分 - 你的类是否支持多个行为(接口),还是作为一种类型的东西(基类)?

There is also the question of how the base functionality is implemented. 还存在如何实现基本功能的问题。 If you are using an approach like the template method pattern where you have common implementation code for all derived classes, you will want to use an abstract base class. 如果您使用的方法类似于模板方法模式 ,其中您有所有派生类的通用实现代码,那么您将需要使用抽象基类。

Here is good article explaining concept of interface and why to use it and how to declare abstract classes in PHP.Though it does not differentiate when to use both concepts. 这里有一篇很好的文章解释了接口的概念以及为什么要使用它以及如何在PHP中声明抽象类。尽管它没有区分何时使用这两个概念。
Understanding and Applying (Interfaces) Polymorphism in PHP 理解和应用(接口)PHP中的多态性

when you bound for certain functionality is required--use interface.but if you want some optional functionality --use abstract class . 当你需要某些功能时 - 使用interface.but如果你想要一些可选功能 - 使用抽象类。 for example if isi(indian standard institute) makes a standards for computer monitor with functionality(it should have)--Display,power button,contrast setter. 例如,如果isi(印度标准协会)制定了具有功能的计算机监视器标准(它应该具有) - 显示器,电源按钮,对比度设置器。 if all 3 functionality is compulsory then use interface, which bound monitor manufacturer company to implement all 3 functionality. 如果所有3个功能都是强制性的,那么使用接口,它绑定监视器制造商公司以实现所有3个功能。 if any is optional then use abstract class. 如果any是可选的,那么使用抽象类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM