Interface Default Methods: a PHP proposal

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ม.ค. 2025

ความคิดเห็น • 43

  • @NedumEze
    @NedumEze ปีที่แล้ว +5

    Anything to make our lives better, easier. Enable us spend less time scripting and more time lounging. Anything that enables you understand my codes and vice versa. Am all for it.

  • @Darkray201
    @Darkray201 ปีที่แล้ว +4

    I bursted into laughter when I realized there is a subreddit for desired paths 🤣 As always, great video. Presented in a humorous, yet concise and informative manner. Thanks for that!

    • @phpannotated
      @phpannotated  ปีที่แล้ว +1

      It's actually how I learned about the concept 🤣 Someone in a Reddit comment pointed to that sub 😅

  • @AftabHussainMiranda
    @AftabHussainMiranda ปีที่แล้ว +1

    At the beginning I hated the idea of interface default methods, but with this short video and the example of the paths you kind of sold me the idea, might need to rethink more about the cons.

  • @ChristianWolfPlus
    @ChristianWolfPlus ปีที่แล้ว +1

    Was skeptical at first but this video, and realizing that Rust also does it, showed me the benefits.

    • @phpannotated
      @phpannotated  ปีที่แล้ว

      Rust does it with traits, but the basic idea is the same :)

  • @martinh4982
    @martinh4982 ปีที่แล้ว

    I'd have to try it in my own code to see if, how and where I would use them.

  • @savedrotex
    @savedrotex ปีที่แล้ว

    How would that work with testing? Would you need to create an empty class that extends the interface with default methods to test the Interface's default method implementations or would you be able to now create instances of such Interfaces to test their default method implementations?

    • @phpannotated
      @phpannotated  ปีที่แล้ว +1

      Sure, that isn't any different from how I test now when using interfaces + traits.

    • @savedrotex
      @savedrotex ปีที่แล้ว

      @@phpannotated thanks

  • @victortodoran1828
    @victortodoran1828 ปีที่แล้ว +5

    The one thing we DO NOT need is more Inheritance.

    • @Yogarine
      @Yogarine ปีที่แล้ว

      Agreed. This problem has already been solved with composition.
      There is a reason these best practices were invented: to protect programmers from inventing “clever” tricks which will bite them in future.
      Unfortunately, programming language developers are starting to cater more and more towards convenience instead of just teaching people to use better patterns.
      Traits are nice because they don’t provide any native way to check for their usage in an object. They’re just code templates. This stimulates the use of Interfaces because you’ll want to also provide an interface for the logic you put in your trait.
      Like traits, composition also incentivises the use of interfaces, because if you don’t use an interface it can be really hard to replace a class with your own without extending it.

  • @feldinho
    @feldinho ปีที่แล้ว +2

    I still can’t see why we can’t simply have straight multiple inheritance…

    • @JonathanAlexeyPereiraFontes
      @JonathanAlexeyPereiraFontes ปีที่แล้ว

      I'm not sure, but probably because of Diamond Inheritance Problem, something like that.

    • @feldinho
      @feldinho ปีที่แล้ว

      @@JonathanAlexeyPereiraFontes While this is a question, it's not a problem.
      Python solves this by having a well defined precedence for inherited methods. An exception would also be acceptable.

    • @MetrolMC
      @MetrolMC ปีที่แล้ว

      @@JonathanAlexeyPereiraFontes Don't you also have to address that very same thing with multiple traits? To keep this on topic, the very same problem shows up with interfaces once you put functionality in place.

    • @JonathanAlexeyPereiraFontes
      @JonathanAlexeyPereiraFontes ปีที่แล้ว

      @@MetrolMC Trait is like copy and paste methods. Not exactly same.

  • @JMRVRGS
    @JMRVRGS ปีที่แล้ว

    Can you make a Modern PHP tutorial like from beginner to advanced? With best practices, OOP, MVC, Projects, etc.
    Can anyone recommend me a good resources to learn php in 2023?

  • @crazyingenieur3277
    @crazyingenieur3277 ปีที่แล้ว +1

    I am in favor of this proposal, default implementation of an interface can be very useful.
    What do you guys think about being able to have properties in interfaces? Would that be a good or bad idea?
    And how about a clash mechanism for cases like for instance when a class implements two interfaces that have the same method?

    • @phpannotated
      @phpannotated  ปีที่แล้ว +1

      I think that since we got readonly properties, it makes sense to allow them in interfaces as well.

    • @solobot
      @solobot ปีที่แล้ว

      @@phpannotated I think we already have a traits for that. We should just allow traits to implement interfaces and leave just interfaces alone, or probably let interfaces use traits

  • @cautionbug
    @cautionbug ปีที่แล้ว

    Traits get a bad reputation because they enable lazy code with a bunch-of-functions mentality. But they don't have to be bad.
    i recently overhauled an application with a lot of old junk code that followed no patterns at all. But because i had limited time and compatibility concerns (and language limit of 7.4), i leveraged traits to simulate a container and DI structure. An object is managed by a trait, and dependency classes that use the trait can receive the object from the dependent class. Since objects are handed off by reference, only one instance is passed around unless you explicitly create a new one.
    It's not the cleanest thing ever written, but it solved the real problem i had, pushed our code quality up, maintenance demand down, and gave us a small point of transition to rewriting into an established framework later.
    i would love to see default implementations to get the strength of an intentional design with the efficiency of boilerplate reduction. My interface may say "paintYellowStripe" but i can't force the implementation to actually do what it implies. A default implementation at least lets me define the function and its intended operation, letting the final implementation use it or replace it. Right now traits do some of this, but weaken the design.

  • @SpliffSplendor
    @SpliffSplendor ปีที่แล้ว

    Thanks! I'm absolutely with you. Let's hope it will be implemented .

    • @TheRafark
      @TheRafark ปีที่แล้ว

      It didn’t pass.

  • @stefanosgiannakis7151
    @stefanosgiannakis7151 ปีที่แล้ว +8

    Nope I don't like this change. It's a hack for the single inheritance that php uses. There is a concept behind single inheritance and why interfaces have to be simple implementation contracts

  • @MartinsTalbergs
    @MartinsTalbergs ปีที่แล้ว +1

    We need generics!!!

  • @babatundeojerinde
    @babatundeojerinde ปีที่แล้ว

    Traits are a life saver! I can’t imagine having to copy code across several implementations of an interface. This rfc will make a whole load of sense if implemented.

  • @klimaluky
    @klimaluky ปีที่แล้ว

    Nothing wrong with that. Rust has only traits and it's doing exactly this thing.
    It's kinda more functional approach of composition multiple blocks in one class shell.
    But as always, if someone doesn't like it in the end, they just can use interfaces by theirs 'best practises'.

  • @SaiyanJin85
    @SaiyanJin85 ปีที่แล้ว +1

    Having the ability to provide a default implementation in the interface is just another tool for us, obviously we don’t have to use it for every method and to be honest writing a separate trait to pair it with an interface default implementation is a waste of time since this trait should be used only for this particular interface.

  • @TravisFont
    @TravisFont ปีที่แล้ว

    Might as well create contracts RFC then

  • @christophrumpel
    @christophrumpel ปีที่แล้ว

    Great one 👍

  • @0zankurt
    @0zankurt ปีที่แล้ว

    Extremely needed!

  • @AndrzejKupczyk
    @AndrzejKupczyk ปีที่แล้ว +1

    While I generally agree, I have a concern that this particular RFC might be a step backward instead of an envolvement. Thanks for the video!

  • @mulatto401
    @mulatto401 ปีที่แล้ว +1

    100% disagree! Let us not follow and copy what Java did with this bad language design. We should keep a clear separation of the contract and the implementation details. This also increases the amount of Interface segregation principle violations in the code.

    • @phpannotated
      @phpannotated  ปีที่แล้ว

      Java, Kotlin, Swift, Rust, and C# support interface default methods.
      Are they all wrong?

  • @srenravnlund1991
    @srenravnlund1991 ปีที่แล้ว

    I like it!

  • @solobot
    @solobot ปีที่แล้ว

    Why not just let traits implement interfaces? Like trait A implements B,C and any class using that trait can be used in type hints like fn(B $class_A)=> $class_A->stuffFromBIntreface()

    • @phpannotated
      @phpannotated  ปีที่แล้ว +1

      It might be a less groundbreaking solution. You'd still need two files in that case though.

  • @lucasayabe
    @lucasayabe ปีที่แล้ว

    We already have traits for implementing multi-inheritance, we don't need default methods, one of the points that makes interfaces good, is that interfaces don't provide any implementation detail, so you can depend on them without worries about how that thing will be implemented.
    Java and C# have default methods because they don't have traits (and i think that is bad in them too), if you really want this feature to avoid the duplication of code separated in Interfaces and Traits, just make a RFC to implement multi-inheritance in the PHP classes, don't mess with the interfaces as they are doing fine.
    The case of success of horizontal inheritance that i see in Traits, that can be enhanced by this RFC as you can make them combine with the power of interfaces, is of the Rust traits, but at this point, its a advantage of functional programming and not of OOP, soo, give me type classes with generics instead of a half of a poor version of this feature that brings problems for something that really doesn't need.
    If PHP really takes this path, i, at least, wish they make this optional with an attribute or keyword similar to final, that prevents an interface to have default methods and preserve they original meaning and garanties.

  • @ward7576
    @ward7576 ปีที่แล้ว +3

    I am certainly annoyed by "git gud" type of developers saying that best practices aren't rules... yeah, sparky, let me throw a spaghetti code towards you that you shouldn't refactor as best practices are not rules, and keep maintaining just for the sake of handling incoming deprecations.

    • @phpannotated
      @phpannotated  ปีที่แล้ว +1

      I hope you understood that I'm not against best practices, and that I'm for carefully considering and re-evaluating them?

    • @ward7576
      @ward7576 ปีที่แล้ว +1

      @@phpannotated hope you understood I am not talking about you, baby face