Use an interface when you want to force developers working in your system (yourself included) to implement a set number of methods on the classes they'll be building.
Use an abstract class when you want to force developers working in your system (yourself included) to implement a set numbers of methods and you want to provide some base methods that will help them develop their child classes.
Another thing to keep in mind is client classes can only extend one abstract class, whereas they can implement multiple interfaces. So, if you're defining your behavior contracts in abstract classes, that means each child class may only conform to a single contract. Sometimes this a good thing, when you want to force your user-programmers along a particular path. Other times it would be bad. Imagine if PHP's Countable and Iterator interfaces were abstract classes instead of interfaces.
One approach that's common when you're uncertain which way to go (as mentioned by cletus below) is to create an interface, and then have your abstract class implement that interface.
Answer from Alana Storm on Stack OverflowUse an interface when you want to force developers working in your system (yourself included) to implement a set number of methods on the classes they'll be building.
Use an abstract class when you want to force developers working in your system (yourself included) to implement a set numbers of methods and you want to provide some base methods that will help them develop their child classes.
Another thing to keep in mind is client classes can only extend one abstract class, whereas they can implement multiple interfaces. So, if you're defining your behavior contracts in abstract classes, that means each child class may only conform to a single contract. Sometimes this a good thing, when you want to force your user-programmers along a particular path. Other times it would be bad. Imagine if PHP's Countable and Iterator interfaces were abstract classes instead of interfaces.
One approach that's common when you're uncertain which way to go (as mentioned by cletus below) is to create an interface, and then have your abstract class implement that interface.
The differences between an Abstract Class and an Interface:
Abstract Classes
An abstract class can provide some functionality and leave the rest for derived class.
The derived class may or may not override the concrete functions defined in the base class.
A child class extended from an abstract class should logically be related.
Interface
An interface cannot contain any functionality. It only contains declaration of the methods.
The derived class MUST provide code for all the methods defined in the interface.
Completely different and non-related classes can be logically grouped together using an interface.
When to use abstract classes vs interfaces and traits?
Base Class & Interfaces or Abstract Classes - I'm lost when to use what
Any reason to use an interface over an abstract class?
When should I use an interface/abstract class?
An interface/abstract class basically guarantees that an inheriting class will define a particular method.
Here's an example which isn't necessarily PHP, but easy to understand. Say you're making a space shooter game, where you have ships, asteroids and bullets.
Though they're different, they share many things in common: They need to move, they need to be rendered, they need to handle collisions.
Rendering is the same for all of these objects: simply draw the object at its current co-ordinates on the screen.
Collisions and movement are different though: a bullet moves in a straight line and disappears upon collision. An asteroid moves slowly, while rotating, and explodes into smaller asteroids when hit. A ship's movement requires input and more complex physics.
You could use an abstract 'GameEntity' class to define a 'render()' method, which does the same for each entity, and abstract methods for 'movement()' and 'collision($with)', which are defined by the implementing classes themselves. Then you can use a simple loop to iterate through all of your GameEntity objects calling render(), movement() and collision(), knowing that the methods exist, without having to know what type of entity you're dealing with.
The advantage of this is that if you want to add a new entity, such as a UFO, you simply inherit the abstract class and you know that your existing code will be compatible without modification specifically to account for the UFO class.
More on reddit.comVideos
I came across this answer for how to use interfaces and traits in unison: http://stackoverflow.com/a/9205347
Could someone explain to me or suggest some reading material as to how abstract classes fit into that answer? Do interfaces and traits used together remove the need for abstract classes?
When is it best to use abstract classes over interfaces and traits? When is it best to use interfaces and traits over abstract classes?
Edit: Thanks to everyone who responded. You have been very welcoming to a new redditor.
I understand a base class provides common methods to other classes that extend it.
And from what I've read, Interfaces provide a way to make sure that a method exists in your class.
It seems that Abstract class combines the two into a single file, so I'm a bit confused on when to use what.
Let's say I have a method called get_device() that is shared amount LaptopDevice, DesktopDevice, and MobileDevice classes.
Each device class gets a list of devices from its device type.
Would I do something like:
abstract class AbstractDevice {
public function get_device() {
return $this->device;
}
}
class MobileDevice extends AbstractDevice {
}or this
class Device implements DeviceInterface {
public function get_device() {
return $this->device;
}
}
class MobileDevice extends Device implements DeviceInterface {
}