1/24 Single-responsibility principle
Each class should have only one reason to change.
A great example is data and its representation. Usually, they change for different reasons. Hence it's probably a safe bet to decouple the query layer from the representation layer.
Each class should have only one reason to change.
A great example is data and its representation. Usually, they change for different reasons. Hence it's probably a safe bet to decouple the query layer from the representation layer.
7/24 SRP
Now If a PM says: "can we please change the definition of 'most popular posts'?" I know I need to go to the Post model. And we also saved the User model. But we can easily have the same problem with the Post model as well.
Now If a PM says: "can we please change the definition of 'most popular posts'?" I know I need to go to the Post model. And we also saved the User model. But we can easily have the same problem with the Post model as well.
10/24 Open-closed principle
A class should be open for extension but closed for modification
It sounds weird, I know. So let me show you an example.
A class should be open for extension but closed for modification
It sounds weird, I know. So let me show you an example.
11/24 OCP
We're working on a social app with users, posts, comments, and likes. Users can like posts, so you implement this feature in the Post model. But now, users also want to like comments. You have two choices:
We're working on a social app with users, posts, comments, and likes. Users can like posts, so you implement this feature in the Post model. But now, users also want to like comments. You have two choices:
14/24 OCP
This is pretty standard, right? But think about what happened here.
We just added new functionality to multiple classes without changing them!
We extended our classes instead of modifying them. And this is a huge win in the long term.
Traits are cool!
This is pretty standard, right? But think about what happened here.
We just added new functionality to multiple classes without changing them!
We extended our classes instead of modifying them. And this is a huge win in the long term.
Traits are cool!
15/24 OCP
My upcoming book “Laravel Concepts” will contain a much more complicated example of OCP using products, their different variations, and orders.
You can join the waiting list here:
laravel-concepts.io
My upcoming book “Laravel Concepts” will contain a much more complicated example of OCP using products, their different variations, and orders.
You can join the waiting list here:
laravel-concepts.io
17/24 LSP
We have an abstract EmailProvider and we use both MailChimp and ConvertKit for some reason. These classes should behave exactly the same way, no matter what.
This sounds obvious, however, there are some important thing that needs to be satisfied:
We have an abstract EmailProvider and we use both MailChimp and ConvertKit for some reason. These classes should behave exactly the same way, no matter what.
This sounds obvious, however, there are some important thing that needs to be satisfied:
18/24 LSP
- Same method signatures with the exact same types
- Same return types. If you return an array you should use the exact same shape! Or use DTOs instead.
- The same exceptions should be thrown from each method
- Same method signatures with the exact same types
- Same return types. If you return an array you should use the exact same shape! Or use DTOs instead.
- The same exceptions should be thrown from each method
23/24 DIP
So every class should depend on the abstract MarketDataProvider not on the concrete implementation.
Fortunately, this is pretty standard in Laravel thanks to its awesome service container.
So every class should depend on the abstract MarketDataProvider not on the concrete implementation.
Fortunately, this is pretty standard in Laravel thanks to its awesome service container.
24/24 Thank you!
Thank you if you’re still here!
I’m working on a book called “Laravel Concepts” which will go into MUCH more detail on the SOLID principles and other concepts.
laravel-concepts.io
Thank you if you’re still here!
I’m working on a book called “Laravel Concepts” which will go into MUCH more detail on the SOLID principles and other concepts.
laravel-concepts.io
Loading suggestions...