#5: Dependency inversion principle (DIP) in PHP, Laravel | SOLID Design Principles

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] hello everyone welcome to our channel my name is harish kumar finally in this lesson we come to letter d in solid design principles this stands for dependency inversion dependency inversion is broadly about decoupling concrete classes by preventing those classes having any direct references to each other let's see its definition the dependency inversion principle state that high level modules should not depend on low level modules both should depend on abstractions and abstractions should not depend on details details should depend on abstractions confused right when we heard a definition like that it just basically fly right over our head what this definition trying to say what are high level modules and what are low level modules what are abstractions and how we depend on them let's see an example to understand this so let's say you are implementing a payment process so let's create a payment process class payment process dot php and class payment process and let's say this payment process class has a construct method now let's say this construct method going to accept a payment method let's say it is going to accept stripe payment so first let's create a style payment class stripe payment method dot php and let's say it has method make payment now in this payment process class right here in this construct method it is going to accept stripe payment method and next here i'll say download this payment method is equal to dollar method and let's say it has protected dollar payment method now according to dependency inversion principle high level modules should not depend on low level modules now pause the video and compare these two classes and try to find out which class is high level and which one is low level if your answer is payment process is high level class and stripe payment is low level class then your answer is right a high level module or class is a any class or module that contains the business model of an application a high level modules are primarily consumed by the presidential layer within an app on the other hand low level modules are consumed by the high level modules so in this example if you see payment process here in the construct method it is accepting strike payment method so this payment process is high level module and this try payment method is a low level module now according to the definition of dependency inversion the high level modules should not depend on low level modules but here in this payment process in the construct method it is accepting stripe payment method which is the concrete implementation of a class and that violates the dependency inversion principle that is because let's say you also want to add another payment method let's say paypal so let's create this paypal payment method class payment method and it has method make payment now let's say somewhere in your application you have utilized this class something like this dollar payment process is equal to new payment process class it is going to accept new stripe payment method and then you can call any method of this payment process for now let's say pay method so let's create this method pay and this method going to call this payment method make payment like this now right here you can see this payment process except stripe payment method because in the construct method it depends on strike payment method but if we want to switch to paypal payment method we cannot do this we cannot switch to paypal payment method because of this dependency so to accept both strike payment method and paypal payment method here in the construct method it should depend on abstraction rather than a concrete class and the abstractions are interfaces so here we should create a payment method interface and then this construct method should depend on payment method interface so let's create this payment method interface php interface payment method interface and this interface has public function make payment so now right here it should depend on payment method interface and both these classes are low level modules so it should also depend on abstraction that is interfaces so in the paypal payment method let's implement payment method interface and similarly this stripe payment method should also implement payment method interface and now this is valid we can switch strike payment method or paypal payment method according to the requirement of the application now this payment process is a high level module and it depends on abstraction payment method interface similarly the low level classes paypal payment method and stripe payment method also depends on abstraction payment method interface now here you should also note that the high level modules are going to accept the abstraction on the other hand the low level modules going to implement the abstractions now here both high level and low level modules are depending on abstraction that abstraction is payment method interface the dependency inversion also says that the abstraction should not depend on details the details should depend on abstraction so what is the meaning of that how this abstraction depends on detail and this part is little tricky to understand here detail means the implementation logic in this law level module so this abstraction should not depend on the implementation detail of this low level module what it is trying to save if you change the details in this low level module then it should not affect this abstraction if you make the changes right here in the abstraction then possibly you may need to modify the high level module also in conclusion what this principle says never depend on any concrete class a class should only depends on abstraction or interface and high level module should not depend on low level module both should depend on attractions so it should allow to change any implementation easily without altering the high level code if your code does not follow this principle can be too coupled which means you will have a hard time managing the project this is all in this video and thanks for watching if you like the video hit the like button share this video and don't forget to subscribe us see you in the next lessons [Music] you
Info
Channel: QiroLab
Views: 4,313
Rating: undefined out of 5
Keywords: Dependency inversion Principle, Dependency inversion principle for classes, Dependency inversion principle for interface, why use Dependency inversion principle, why Dependency inversion principle is important, solid php, solid laravel, solid design principles examples, solid design principles in software engineering, solid design principles interview questions, php interview questions, laravel interview questions, learn with qirolab, qirolab
Id: QE6HN8evtnw
Channel Id: undefined
Length: 10min 12sec (612 seconds)
Published: Thu Apr 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.