简体   繁体   English

如果你可以使用接口和特性,为什么PHP有抽象类?

[英]Why does PHP have abstract classes if you can use an interface and traits?

Earlier today I was doing research on PHP's abstract classes , interfaces , and traits . 今天早些时候,我正在研究PHP的抽象类接口特性

As far as I can tell, an abstract class says "anything using me will be using these methods and attributes", interfaces say "anything using me must have these methods and attributes", and traits say "anything using me will also have these methods and attributes". 据我所知,一个抽象类说“任何使用我的东西都将使用这些方法和属性”,接口说“任何使用我的东西都必须有这些方法和属性”,并且特征说“任何使用我的东西也会有这些方法”和属性“。

Now, my question is, if you get the equivalent of an abstract class when you use an interface and a trait, why are there abstract classes? 现在,我的问题是,如果在使用接口和特征时获得等效的抽象类,为什么还有抽象类?

If I'm wrong and an interface and a trait aren't the equivalent of an abstract class, can you please explain why that's not the case? 如果我错了,界面和特征不等同于抽象类,你能解释一下为什么不是这样吗?

I think there is some philosophical difference on how and when to use them. 我认为在如何以及何时使用它们方面存在一些哲学上的差异。

You said : 你说 :

  1. abstract classes: "anything using me will be using these methods and attributes" 抽象类:“使用我的任何东西将使用这些方法和属性”
  2. interfaces:"anything using me must have these methods and attributes" 接口:“使用我的任何东西必须具有这些方法和属性”
  3. traits: "anything using me will also have these methods and attributes". 特征:“任何使用我的东西都会有这些方法和属性”。

If you focus on your own wordings it makes sense. 如果你专注于你自己的措辞,这是有道理的。

Abstract Classes are in reality define things that are abstract eg Vehicle is an abstract thing until or unless its materialized in the form of a car or a bike . 抽象类是在现实中定义的东西都是抽象的如汽车是一个抽象的东西,直到或者除非其物化在汽车或自行车的形式。 Neither interface define it nor traits. 接口既不定义它也不定义特征。

Interfaces compliment the class inheritance functionality where a class inherits from multiple classes(only certain languages provide multiple inheritance eg C/C++). 接口补充了类继承功能,其中一个类继承自多个类(只有某些语言提供多重继承,例如C / C ++)。 Interfaces , as names suggest focus on the INTERFACE , and not the implementation of the interface method in class which is implementing it. 接口,如名称所示,侧重于INTERFACE,而不是实现它的类中接口方法的实现。 It makes classes PLUG & PLAYABLE so everyone should follow a standard . 它使类PLUG&PLAYABLE所以每个人都应该遵循标准 If you further read about factory and adapter pattern on OOP you will understand it. 如果您在OOP上进一步了解工厂和适配器模式,您将理解它。

Traits have implementation/functionality that is not bound to specific classes.Instead it could be found across different classes. 特征具有未绑定到特定类的实现/功能。相反,它可以在不同的类中找到。 Its like a gene in genetics which stays mute in parents and appear only in certain children. 它就像遗传基因一样,在父母身上保持沉默,只出现在某些孩子身上。 Or to be concise selective inheritance but not bound to single class. 或者是简洁的选择性继承,但不限于单个类。 So it provides a way much better code-reuse 因此,它提供了一种更好的代码重用方式

Edit Interface + Trait != Abstract Class , because when using Trait inheritance is selective as you select specific trait to use and while using Abstract Class inheritance is mandatory or dictated by parent class you don't have freedom! 编辑接口+ Trait!=抽象类,因为当使用Trait继承是selective因为您选择要使用的特定特征,而使用Abstract Class继承是强制性的或由父类决定您没有自由!

It's a bit like saying if floats exist, why do integers exist, they both serve their own specific purposes, but more importantly a look at the history of PHP will shed some light on this: 这有点像说浮点数是否存在,为什么存在整数,它们都有自己的特定用途,但更重要的是看看PHP的历史将会对此有所了解:

PHP was initially built without any support at all for classes, and over the years more and more has been added to extend the capabilities of PHP as we push it further and further (and arguable play catch up with other OOP based languages) PHP最初是在没有任何支持的情况下构建的,并且多年来越来越多地添加了PHP以扩展PHP的功能,因为我们进一步推动它(并且有争议的游戏赶上其他基于OOP的语言)

PHP 5.0 PHP 5.0

2004 - Abstract classes and interfaces introduced 2004年 - 介绍了抽象类和接口

PHP 5.4 PHP 5.4

2012 - Traits introduced 2012年 - 引入了特征

That's an 8 year break, in that time among other things the need arose to add support for interfaces with implementations , and so the trait was born 那是一个8年的休息时间,在那个时候需要增加对实现接口的支持,因此特性诞生了

When you use traits, you simply require a given class to implement some methods. 当您使用traits时,您只需要一个给定的类来实现某些方法。

You won't inherit any properties or methods and you won't force the object into any inheritance tree. 您不会继承任何属性或方法,也不会强制该对象进入任何继承树。

Thus you can have several completely unrelated classes using the same trait, just to guarantee any object of theses classes will support a given method. 因此,您可以使用相同的特征来拥有几个完全不相关的类,只是为了保证这些类的任何对象都支持给定的方法。

this is how PHP does the famous Mixins. 这就是PHP如何着名的Mixins。 Basically a Mixin is just a class that can share common traits with several other classes. 基本上,Mixin只是一个可以与其他几个类共享共同特征的类。 Traits allow to enforce that constraint on the methods of classes, independently of the way these classes inherit from each other or not. Traits允许对类的方法强制执行该约束,而与这些类是否相互继承的方式无关。

Instead of having to do multiple inheritances to the point of silliness when you want a class to combine the behaviour of two parents, you can use traits to obtain the same result without the hassle of ineriting a bunch of unwanted other things. 当你想要一个类来组合两个父母的行为时,你可以使用特征来获得相同的结果,而不必麻烦一些不需要的其他东西,而不是必须做多次继承到愚蠢的点。

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

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