Laravel Contracts and PHP Interfaces: Explained with Two Examples

Sdílet
Vložit
  • čas přidán 25. 07. 2024
  • Interfaces are one of the most confusing topics in OOP with Laravel. When/how specifically should they be used? Let's take a look at two examples.
    Related links:
    - Laravel Examples of Interfaces: laravelexamples.com/tag/inter...
    - Laravel Service Providers: All You Need To Know • Laravel Service Provid...
    - Official Docs: List of Laravel Contracts laravel.com/docs/8.x/contracts
    - - - - -
    Support the channel by checking out our products:
    - Enroll in my Laravel courses: laraveldaily.teachable.com
    - Try our Laravel QuickAdminPanel: bit.ly/quickadminpanel
    - Purchase my Livewire Kit: livewirekit.com
    - View Laravel Code Examples: laravelexamples.com
    - Subscribe to my weekly newsletter: bit.ly/laravel-newsletter
  • Jak na to + styl

Komentáře • 40

  • @thelostrider1
    @thelostrider1 Před 3 lety +13

    Nice video. When using Laravel people seem to forget that Laravel is PHP and end up don't learning OOP or "pure PHP" (mostly because it's kinda easy to use Laravel without much knowledge of PHP). That's something I don't like, even tho I'm learning Laravel I feel the need to learn more about pure PHP and OOP in general

  • @dclindner
    @dclindner Před 3 lety +10

    Given PHP's limit of extending from only one class, you can't rely on on abstract class, necessarily, if you're providing code to be used by others (such as open source packages). it seems common to define an interface with the methods required, and include a trait that defines those methods. I found this confusing at first, but very helpful to add functionality while working around the one-class extends limit.

  • @ajithlal1688
    @ajithlal1688 Před 3 lety +1

    I was struggling to understand interface and this video gave me a better understanding. Thank you ❤️

    • @L-A-O-S
      @L-A-O-S Před 3 lety +1

      There are still abstract classes awaiting you, which will only create more confusion. )

  • @linkernetir
    @linkernetir Před rokem

    thank you for the best explanation about contracts in laravel.

  • @deveshmaurya9101
    @deveshmaurya9101 Před 2 lety

    Thanks for posting

  • @flashandrew6772
    @flashandrew6772 Před 3 lety +4

    Thanks for the video! It also can be great to see some videos like “middle” level of difficulty. For example something about compare ORM and CQRS. Or maybe some explanation about concepts of DDD

    • @lucasj.pereira4912
      @lucasj.pereira4912 Před 3 lety +2

      Also websockets

    • @LaravelDaily
      @LaravelDaily  Před 3 lety +3

      For now, I'm focused on practical topics and not theoretical concept comparison. Maybe, when I run out of practical topics, I'll switch to the philosophy on this channel :)

  • @haroldpepete
    @haroldpepete Před 3 lety

    Thank sr for share

  • @VadimBesedin
    @VadimBesedin Před 3 lety +9

    So, as i understand, interface would be good for situation when i need to process payment. One interface and several payment provider classes will be implementing that interface. Also later i will be able to add just another payment provider class and it will work with present interface. Right?
    It's like USB, you can connect different devices to it, and they will work normally because they implement this USB interface.

  • @msdeav
    @msdeav Před 3 lety

    thank you, sir..

  • @lma_cristian
    @lma_cristian Před rokem

    Now I understand it better.

  • @sale7680
    @sale7680 Před rokem

    Thank u 🙏

  • @itone3438
    @itone3438 Před 2 lety +1

    thanks

  • @jailsoncarneiro4936
    @jailsoncarneiro4936 Před 2 lety

    Show de Bola!

  • @L-A-O-S
    @L-A-O-S Před 3 lety +1

    Oh, OOP is already here) Cool, well then you should take into account that there are also abstract classes. It's just difficult to rebuild your logic in order to start using all this, although the advantages are immediately felt. And looks like interfaces should be named like gates or politicians? liked the topic of using oop in laravel

    • @LaravelDaily
      @LaravelDaily  Před 3 lety +1

      Ok adding the interface and abstract classes to the to-do list.

    • @Bevallalom
      @Bevallalom Před 3 lety

      Laravel is PHP OOP mostly..... Just saying.

  • @vivekwah
    @vivekwah Před 3 lety +6

    I always have trouble deciding to use either interface or abstract class. Can you please help with this confusion.

    • @LaravelDaily
      @LaravelDaily  Před 3 lety +8

      Ok adding the interface and abstract classes to the to-do list.

    • @igoreksazonov
      @igoreksazonov Před 3 lety

      its up to your mind

    • @vivekwah
      @vivekwah Před 3 lety +1

      @@igoreksazonov yes till now I have been using with that mindset only. But in some good repos I've noticed in some places abstract classes are used and some places interfaces. I only have difference with respect to inheritance. Using abstract class we restrict extend with that class whereas in case of interface there can be many interfaces implemented.

    • @kaczuszka-dt
      @kaczuszka-dt Před 3 lety +5

      Abstract classes do offer some functionality, its not just structure, its the actual functionality with something in already.
      Interfaces offer only and only description of what the class should be capable of, we cannot give it any real properties or functionality, just a shared set of methods

  • @damilaresamuelfagbenro1881

    I have got a question. Is it possible to have the CanCaptureScreenshot binding with both classes in the service provider?

    • @LaravelDaily
      @LaravelDaily  Před 2 lety

      Why would you need that? How would Laravel then know which class to run?

  • @eduardoromao
    @eduardoromao Před rokem

    Can be named “API”. Its more natural above Interface and Contract.
    API provides a contract for another service. So contract and API is the same thing, but i’m below API term is already used in hardware and software like a standard, not needing new term like contract.

  • @aymenmohammed2625
    @aymenmohammed2625 Před 3 lety

    ❤😍

  • @pasizdobrekuce
    @pasizdobrekuce Před rokem

    Dependency inversion FTW. 🙌

  • @free2idol1
    @free2idol1 Před 9 měsíci

    I noticed that the author used this function to name his file: $filename = uniqid().'_'.Str::random(30) . ".png";
    => if you loop through 10 files and using above function to name your file, is there any chance that you would get duplicate file name?

  • @devrajin5795
    @devrajin5795 Před 3 lety

    first viewer :D

    • @L-A-O-S
      @L-A-O-S Před 3 lety

      Firster then video duration)

  • @glowiever
    @glowiever Před 2 lety

    basically, php is like java in this regards

    • @fangfang3366
      @fangfang3366 Před rokem

      We should say both of them follow OOP, even like c# LOL

  • @Jagdish0724
    @Jagdish0724 Před 3 lety

    User hasMany(cities)
    City hasMany(Users), hasMany(Restaurants)
    Restaurants belongsTo(City)
    Orders belongsTo(Restaurants)
    now there are city wise admins. admins can see orders of their assigned cities only with global scopes to other cities records .
    How to create this relationships.

  • @soniablanche5672
    @soniablanche5672 Před 3 lety +1

    interface is the opposite of trait, instead of being provided with methods you have to implement them yourself.

  • @Laflamablanca969
    @Laflamablanca969 Před 3 lety +5

    The interface for the relationship is pointless. It’s just adding more complexity than is needed.