#1: Single Responsibility Principle (SRP) 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 the first principle in the solid design is single responsibility principle it represents as in solid the single responsibility principle deals with the classes that try to do too much so according to this principle a class should have only one responsibility or in other words you can say a class should have only one reason to change so let's say we have a class sale reports dot php namespace app solid and class sale reports and this class has a method export and what this class does first it fetches the data from the database so here we can say dollar sales is equal to db table from sales table latest get first it has fetched all the data from the database and then it is going to return sales data in csv format here i am not going to write to generate csv format for now just return string csv format like this so if you see this method it is doing two jobs first it has passed the data from the database and then it is going to generate csv format so this method has two responsibilities or you can say it has two reasons to change first for fetching data from the database and second to generate csv format and you know that in software development business requirements always change so for example let's say in future we need to fetch the data between given dates so this method going to accept start date and end date so we need to fetch the sales reports between start date and end date so here i'll say where between created at and then here i'll pass in array start date and end date like this and maybe in future you need to generate a pdf format as well so next here what we will do here we will accept another variable let's say format default it is going to be csv and here we will add a condition if format is pdf then generate pdf format otherwise it should return csv format and after some time in future you may have a requirement for json format as well and for that again you will add another if condition and here you will check if format is json then return json format now this method is doing lot of things first generating data from the database and then return data in different formats if your colleague is going to work on this project and if we see this method it is going to be very difficult to understand what is going on here he has to read this method for many times the modification or bug fixing is going to be very difficult so now let's refactor this so first let's extract this query in the separate method so i'm going to create a new method let's say its name is between and i'm going to copy this from here and here i'll say return this query and it is going to accept start date and date right here now this sale reports method has between method which is going to fetch data between these given date and next here i'll say download this between and pass here start date and end date like this now what about these formats now here you may suggest that similar like this between method create separate method for each format something like this method pdf export and it is going to accept dollar data and then return pdf format something like this and then in this if condition we can say return dollar this pdf export and here we pass sales this one similarly separate method for these format as well it still doesn't feel right because let's say in future you have requirement for another format let's say excel as format and then again you're going to copy this if condition and here check if format is xls then export xls format like this and look at this entire class it is also doing multiple jobs first it is fetching data from the database and then it has different method for different formats this class also violates single responsibility principle because it has multiple responsibility or you can say multiple reasons to change so how we can resolve this here we need to extract some of the logic into dedicated class for example look at this pdf export why we need this method in the sale reports class we should extract this pdf export to its dedicated class so here i'll create a new class let's say pdf export dot php namespace app solid class pdf export and it has one method export which is going to accept data and then return pdf x4 like this and now we no longer need this method right here let's remove it similarly we will create a dedicated classes for each format so let's add more classes for json export dot php and let's copy all of this content and paste it here here i'll say json export and here json export like this add another class csv export dot php and paste all these content here i'll say csv export and here return csv export like this and in the sale reports class we no longer need this export method so here i am going to remove this now you can see this sale reports has only single responsibility to fetch data from the database and these classes has a single responsibility to export data in specific format for these classes we should use interface but for now let's keep it simple i will talk about interface in the next principle now these classes looks very clean and easy to understand what this method is actually doing now if you need to export particular format what we can do let's say in the web.php right here let's say sale reports is equal to new sale reports and let's say we are going to export in pdf format so dollar pdf x4 is equal to new pdf x4 and then here we can say return dollar pdf export and then we can call export method in this method we need to pass sales data so here i'll say dollar sales report between start date let's say 1st jan 2022 and and the date is 31st jan 2022 and if you go to browser localhost 8000 and we get pdf export similarly if we need csv export then here we will use csv export new csv export class and next here we will say csv export and call export method like this and it will work exactly same we get csv export and that looks much better you can see these classes are now very clean and easy to understand now you may ask that single responsibility means a class should have only one method that is not right a class can have multiple methods for example this sales reports class is related to sales report data so this class can have multiple method which are dealing with the sales data for example let's say you need a to fetch data for a particular month so we can create another method for month and it is going to accept a variable month and then here it is going to fetch data for given month similarly if we need to fetch data for particular air and then here we can create another method for here and then accept here variable and fetch data for given year so it can have multiple methods but its responsibility is single related to sales data let's see a one more example for laravel application in the beginning i forgot to mention that i am already in the laravel application so first let's close this and let's see the http controller post controller here we have store method let's see what it is doing first it is validating from data and then it is saving that data in the post table this method is already very simple and easy to understand but let's say it is doing too many things for example let's say it is also handling image upload or or sending email notifications to author followers or other many things so this method going to have too many responsibilities in that case we need to extract some of the logic into the dedicated class for example we can move this request validation into dedicated post request class and these into the post service class so in the terminal php artisan make request let's say class name is store post request and next request store post request and then we can move this logic from here to right here and now we can remove this next here instead of request we can import store post request like this and now for this logic we can create a dedicated post service class so let's say we have a services directory so services and in this services directory we have post service dot php name space is services and class post service class now in this post service let's say we have a method create let's say it is a static method and now from the post controller we can move all this logic right here in this method now let's import this post class and for this request data let's remove it and here we can accept data and pass that data right here like this now here in the controller we can say post service and then we can call create method and right here we need to pass dollar request only title and body or instead of this you can also pass this request like this and right here instead of this data we should accept store post request dollar request and here we can see request only title and worry because a method should have single responsibility so you should create other methods for these responsibilities as well now in the post controller this store method looks very clean and easy to understand what it is doing if your class has too many responsibilities then in future modifications bug fixing or adding new features are going to be very difficult because first you have to read the code multiple time to understand what is going on in the method or you may hesitate to make modification because it may break the code somewhere else once class have a single responsibility they simply become very easy to work with other developers can also easily understand what the class does and bug fixing becomes a lot easier i hope this lesson was easy to understand if you like the video hit the like button share this video and don't forget to subscribe us see you in the next lesson [Music] you
Info
Channel: QiroLab
Views: 10,250
Rating: undefined out of 5
Keywords: Single Responsibility Principle, single responsibility principle for classes, single responsibility principle for interface, why use single responsibility principle, why single responsibility principle is important, single responsibility principle with example, solid php, solid laravel, solid design principles examples, solid design principles in software engineering, php interview questions, laravel interview questions, learn with qirolab, qirolab
Id: OmtLxnjMnlY
Channel Id: undefined
Length: 15min 44sec (944 seconds)
Published: Sun Apr 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.