Reviewing Use Cases3:29 with Alena Holligan
While there is not always an absolute answer for when to use interfaces vs abstract classes, we'll review some use cases to get you started.
Interfaces vs Abstract Classes
You should consider using abstract class if any of these statements apply to your situation:
- You want to share code among several closely related classes
- You expect classes the extends you abstract class will have many common methods or properties
- You require access modifiers other than public such as protected and private
You should consider using interfaces if any of these statements apply to your situation:
- You expect that unrelated classes would implement your interface.
- For example the interfaces, savoryFlavor and Countable are implemented by many unrelated classes.
- You want to specify the behavior of a particular data type, but are not concerned about how that behavior is implemented.
- You want to take advantage of multiple inheritance of type.
Suggestions for Practice
- Take out the interfaces
- Add additional interfaces.
- Don't make the Collection class abstract.
- Add additional abstract classes.
- Add additional abstract methods.
- Add a constant.
- Add another data source, such as XML or CSV.
As you start making more architectural decisions that affect an entire application it is easy to become paralyzed with trying to do something right.
Do your best to keep repetitive code to a minimum
Don't be afraid to try something (you can always re-factor)
Dependency Management with Composer
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. To learn more, check out our course on Dependency Management with Composer
Standards and Best Practices
Standards and Best Practices are a way for developers to share tips and tools that have helped solve common challenges. You can check out this course to learn about some of the PHP Standards and Best Practices.
The PHP Framework Interop Group is a group of established PHP projects whose goal is to talk about commonalities between projects and find ways to work better together. They are "moving PHP forward through collaboration and standards".
You need to sign up for Treehouse in order to download course files.Sign up