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".
This course has looked at different patterns for organizing your code, 0:00 and reducing the mental strain of having to hold so 0:04 many different items in working memory. 0:07 If you'd like some tips on working within the confines of your own brain and 0:10 getting the most out of your learning, check out our bonus series, How to Learn. 0:15 Interfaces have continued to gain popularity in PHP, 0:20 as the use of package management through Composer has become the standard. 0:23 By having standard interfaces package maintainers can more easily build and 0:28 maintain packages for common tasks, such as logging, caching, and HTTP messages. 0:34 The PHP Framework Interop Group or PHP FIG for short, 0:41 has also been a leader in standardizing common interfaces. 0:44 Check out the notes associated with this video to learn more about composer, 0:49 standards and the FIG. 0:54 Now that you've learned about interfaces and abstract classes, 0:55 which should you use, and when? 0:59 While there's no absolute answer for 1:01 every situation, there are some thoughts to get you started. 1:04 When determining if an abstract class is right for your situation, 1:08 you should consider the following. 1:12 Do you want to share code among several closely related classes? 1:15 Do you expect classes the extends you abstract class will have 1:20 many common methods or properties? 1:24 Do you require access modifiers other than public such as protected and private? 1:27 You should consider using interfaces if any of these statements apply to your 1:34 situation. 1:39 You expect that unrelated classes would implement your interface. 1:40 For example the interfaces, savoryFlavor and 1:44 Countable are implemented by many unrelated classes. 1:48 You want to specify the behavior of a particular data type, but 1:53 are not concerned about how that behavior is implemented. 1:58 You want to take advantage of multiple inheritance of type. 2:02 Like all new skills the more you practice the more comfortable you 2:07 become with using them. 2:12 Take the project we build in this course and make your own changes. 2:14 Add another content type. 2:19 I've included some events to get you started. 2:21 Take out the interfaces, add additional interfaces. 2:24 Don't make the Collection class abstract. 2:28 Add additional abstract classes. 2:33 Add additional abstract methods. 2:34 Add a constant. 2:36 Add another data source, such as XML or CSV. 2:38 As you start making more architectural decisions that affect an entire 2:43 application it is easy to become paralyzed with trying to do something right. 2:48 Even experienced programmers who make different choices six, or 2:53 even three months from now than they will today. 2:57 Do your best to keep repetitive code to a minimum but 3:01 don't be overly concerned with perfection. 3:04 The process will help you improve. 3:07 And as you learn more things, 3:09 you discover how more pieces fit together to build the larger picture. 3:11 Don't be afraid to try something, you can always re-factor as many 3:16 times as you want, each time continuing to build your skills. 3:21 Continue to have fun and keep learning. 3:26
You need to sign up for Treehouse in order to download course files.Sign up