문제

Earlier today I was doing research on PHP's abstract classes, interfaces, and traits.

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 e.g 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 e.g C/C++). Interfaces , as names suggest focus on the INTERFACE , and not the implementation of the interface method in class which is implementing it. It makes classes PLUG & PLAYABLE so everyone should follow a standard. If you further read about factory and adapter pattern on OOP you will understand it.

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!

다른 팁

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 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 5.0

2004 - Abstract classes and interfaces introduced

PHP 5.4

2012 - Traits introduced

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

When you use traits, you simply require a given class to implement some methods.

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. Basically a Mixin is just a class that can share common traits with several other classes. Traits allow to enforce that constraint on the methods of classes, independently of the way these classes inherit from each other or not.

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top