Laravel Contracts and PHP Interfaces: Explained with Two Examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today i want to talk about interfaces in php and laravel specifically based on examples working on my laravel examples project and i think it's the best way to learn something from practical examples especially from open source and i will take two examples out of this one out of this list but you can take a look at all of them and i will explain them step by step what are interfaces why you should use them in which cases it's good to know specifically for bigger projects and what are contracts in laravel and what is the difference between laravel contracts and php interfaces let's dive in first if we take a look at the official php documentation because interface is a php term not laravel so interface allows you to create the code which specifies the methods a class must implement sounds pretty complex right but the main thing is specifies methods a class must implement so you kind of give a contract to whatever class is implementing that contract and i will get to the contract word a bit later so interfaces are defined in the same way as class but replacing class with interface so interface is a set of methods and the body of the methods is empty it's just kind of the set of rules what methods should be in the class but without how they are implemented so from boring theory let's dive into practice with the examples first example is stefan's wifi screenly project actually let's take a look on github how popular it is it is 354 stars it's an open source application which lets users create website screenshots through a simple api so you can check that out but the code of that related to interface is this one so imagine you have an interface called can capture screenshot let's zoom in a bit actually can capture a screenshot and imagine you can capture screenshot with various tools like browser capture another capture and we will dive in in a minute so whatever that implementation is you have a rule that there should be a capture method of url to storage url where to store that screenshot right so this is your interface these are your rules what should be in the can capture screenshot and then you have two classes that implement that capture screenshot that can capture screenshots sorry so first one is in memory browser and another one is chrome browser which uses spicy browser short package so in memory browser just takes the screenshot which is screenly class and gives that to storage path of test screenshot so in other words it's kind of a testing class for capturing screenshots or another class which has full implementation of capture is using spotty browser shot with a lot of features and parameters and then returns new screenshot with path so you have again you have one interface set of rules and you have two different classes that implement the same can capture screenshot which means that both of those classes have to have a method of capture with the same parameters with the same things returned according to the interface rules now why would you do that why have two classes if we scroll down the same example there's a file called screenly service provider in general laravel application it could be app service provider or whatever service provider i have a separate video on specifically how service provider works and i will link that in the description below so in the boot of that service provider we actually specify which of those two classes should be used when mentoring can capture screenshot interface so that interface is called resolved or bind to chrome browser class so chrome browser class is this one and then in memory browser is kind of a secondary class so our main used in the application is chrome browser and then whenever we actually need to do the screen capture so there's a service for that and we do constructing of that service using the interface as a dependency and then laravel knows that this interface or contract and again i will get to contracts in a minute should be resolved as chrome browser not in memory browser because we define it here in the provider but in other place you can override that in the test i found an automated test of the same project it uses in-memory browser when you need to kind of fake the data and here as you can see this app binds the same interface to a different class which is in memory browser here so you replace your main chrome browser to in memory browser for testing in this case specifically and this is where i hope for you it starts to make sense so you can replace one class with implementation of the same interface with another class if you need that if you need to fix something if you need to change the provider for example payment provider email provider whatever other way of implementation of the same interface now before we move on to second example let's discuss interface and contract what is the difference so it's basically the same word it's the same thing described in two different words so interface comes from php and some laravel developers instead of doing interfaces they do contracts so if we go to list of all the examples in our interface and laravel examples here we have app interfaces here we have app interfaces in other three examples it's app contracts and here we have app-based contracts and model contracts so it's kind of replaceable thing you can do both and when i googled what is the story behind laravel contracts specifically i found this stack overflow post from 2015 where people describe contracts as just the word that taylor used from the very beginning in laravel to have it more human feel because interface is kind of like general programming word which can mean a lot of things and contract is more like human way and it is used by other developers as well to describe kind of like signed contract like paper contract that you would sign by hand with a pen contract for the rules it's more human-friendly language and also there's a special section about laravel contracts in the official documentation which describes all the contracts of laravel and in itself in laravel framework there are a lot of contracts available but they are nothing more than just interfaces so it's the same word almost replaceable and the second example of interface in laravel or in fact it is contract as well will be illuminar good work repository this one which is under major change at the moment but in the code we found the in develop branch i think it is interface with a few classes of implementation and here you have another reason to use interfaces so if we go back to the official php documentation it says in practice interface serve two purposes so one purpose we discussed to create object of different classes that may be used interchangeably so you can replace in memory browser with chrome browser and vice versa in the first example but another way to use interfaces or another reason is method to accept and operate on parameter that conforms to an interface while not carrying blah blah blah okay it's pretty complex so what is meant here is you have a rule and every other class a base to that rule so let's take a look practically in here so there's an interface has members so there's a relationship members which returns belongs to many and this is a rule and then in that project we have three models three eloquent models which is office which implements members with belongs to many as a result but belongs to many to a table of office user then there is a project which also implements has members and also needs to have a member method but with a different parameter with project user as a pivot table and then the third is a team also implements has members also members also belongs to many so the same set of rules just with a different pivot table so in this case these classes are not interchangeable and you couldn't for example use project instead of a team or something but the goal here as i understand it is for all the eloquent models that implement has members to have a method of members which specifically returns belongs to many and nothing else so for example if someone in the future wants to create some kind of other model with a function member with has many that wouldn't work and it would throw an error because the interface rule is not met so another reason or another purpose of using interface is to have a set of rules which all the classes in the future may be written by yourself in the future or by other developers would obey to those rules i hope it makes more sense now for those of you who didn't really understand the interfaces in php or in laravel and if you want more examples of generally laravel and interfaces you can visit our laravel examples.com currently it's 400 examples on packages patterns collection methods features eloquent and other stuff all for free so you can check them out and if you want to thank me for this free work you can check out one of the three products that you can see on the screen now from my team which are kind of the source of the money revenue and sponsorship for free projects like laravel examples for me subscribe to the channel because i'm shooting videos daily now and see you guys in other videos
Info
Channel: Laravel Daily
Views: 15,952
Rating: undefined out of 5
Keywords:
Id: IrIZ7wiWocg
Channel Id: undefined
Length: 10min 10sec (610 seconds)
Published: Fri Jul 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.