Abstraction in OOP involves extraction of relevant details. Consider the role of a car salesman. There are many types of consumers. Everyone wants to buy a car, no doubt, but each one having difference in their criteria. Each of them is interested in one or two certain features. This attribute varies accordingly. Shape, color, engine power, power steering, price … the list is endless.
The salesman knows all the details of the car but does he repeat the list one by one until someone finds his or her choice? No. He presents only the relevant information to the potential customer. As a result the salesman practices ‘Abstraction’ and presents only relevant details to customer.
Now consider abstraction from the perspective of a programmer who wants a user to add items to list. Abstraction does not mean that information is unavailable but it assures that the relevant information is provided to the user.
PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated and any class that contains at least one abstract method must also be abstract. Remember abstract methods can not define the implementation. On the other hand, object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
Interfaces are defined with the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.
All methods declared in an interface must be public; this is the nature of an interface. I have a .NET background and found the usage of interfaces there almost ubiquitous. And in Laravel, the injection of interfaces to the classes is being seen frequently. So you better get acquainted with the conceptual background.
In Laravel Interface is considered as a Contract.
Contract between whom? And why? Because an interface does not contain any code but it only defines a set of methods that an object implements. Being said that I hope you now understand they are interrelated. We talked about maintaining a library of small classes with clearly defined scopes and it is achievable with the help of Interfaces. As the book progresses you will find a lot of examples of Interfaces; so don’t worry you will find these two words pretty often in the examples. Till then have patience and read on.
For a clear picture I would like to give a small example so that we can understand this property of encapsulated contract behavior of Interface quickly.
Let us imagine I have a ‘Connection’ folder inside a folder called ‘Bengaliana’ in which I have a connection class that would connect us to the database ‘sanjib’ and retrieve some data from respective tables – ‘users’ and ‘tasks’.
I don’t want to make the ‘ConnectionClass’ know our datatarget. All it will do is just get the connection and retrieve the one attribute from a table. The attribute and table name I would like to supply dynamically so that from one method I can retrieve many kind of data. It could be user names or simple task titles etc. That will also conform to the homomorphism nature of our objects.
In Laravel 5, as document says, all major components implement interfaces which are located in the ‘illuminate/contracts’ repository. This repository has no external dependencies. Having a centralized set of interfaces make you free to use alternative optional decoupled classes and do some dependency injection more freely without Laravel Facades. It sets your choices more open and user friendly. Well there are lot of new staffs you will find as you progress and the new features will make your journey absolutely enjoyable. To name a few – there are Route Cache, Middleware, Controller Method Injection and many more.
From Laravel 5 Authenticating users become easier and the user registration, authentication and password reset controllers are now included out of the box so that they can be easily used. Okay, enough introduction. Now it is time to catch the web artisans and do some codes so that we can make some awesome application in future.