PHP Interface - What is PHP Interface and How to Use It

Поделиться
HTML-код
  • Опубликовано: 22 окт 2024

Комментарии • 30

  • @oyinbrakemimichaelmenebray5966

    Very straightforward. Simple to understand

  • @eloquent2200
    @eloquent2200 3 года назад +1

    Thank you for good explanation!

  • @keegers1
    @keegers1 4 года назад +7

    I guess I don't understand. Why write the interface if to still need to call the class/functions of the other class? Even the last part, you still copied and pasted the MySQL class to the new Oracle one.

    • @clevertechie
      @clevertechie  4 года назад +3

      Because the code of the function's implementation in the MySQL and Oracle classes can be different, this is the key thing to understand.

    • @CaptainBradleySmith
      @CaptainBradleySmith 3 года назад +7

      @@clevertechie why code cant be different with same names of functions without that additional interface part?

    • @jareklotz
      @jareklotz 2 года назад +2

      Answer : you can pass to function as a parametr class object and define in parameter, type of interface that object implements. Then, this function knows that this object has all necessary functions, so no error will occur. Example:
      myFunc(InterfaceName $object).

    • @maheraldokh6962
      @maheraldokh6962 2 месяца назад

      If you have to write the function logic of every class method in it's method,then what is the benefit of using that interface?
      Just for keep using the same methods name but be able cloning from a class name to another?

    • @damionvoshall636
      @damionvoshall636 23 дня назад

      Php is not statically typed. It makes it hard to see the benefits, but it does a few things.
      1. Modularity.
      You no longer "hard code" the mysql database. As long as the data provider has a function that "listOrders" that returns an array of "Order" objects, it no longer matters where the data is coming from. Whether it's coming from excel, sql, facebook api, etc. Now it becomes easy to add multiple data sources or swap to a new one.
      2. Mocking.
      When testing, you can now hard code a list of Order objects within the listOrders(). This way, it doesn't matter where the data is coming from. You are able to write proper unit tests. As long as that function returns a valid array of "Orders" your application will work as expected.
      3. Real world example:
      I have a table business. I provide wood. I want to get tables.
      CreateTable(wood): Table
      As long as a manufacturer takes wood and gives a table, it no longer matters what manufacturer I use. The application will JUST WORK.
      I can have multiple manufacturers, or swap out manufacturers easily.

  • @easywiesi2055
    @easywiesi2055 Год назад

    thanks! finally i got it 👍

  • @Ruban_Sn
    @Ruban_Sn 4 года назад +1

    2:41 aren't those supposed to called as "function declarations!?

  • @saulespukje8
    @saulespukje8 3 года назад

    nice, thanks!

  • @ripplesr5655
    @ripplesr5655 Год назад

    I don't seem to grasp the concept of, why would you use Interface if we are using the same name methods inside every classes anyway.

    • @ripplesr5655
      @ripplesr5655 Год назад

      Ok, got it. Basically this helpls you in the development phase by forcing you to adhere to a set of rules that implements the interface. Anyone, coming from TypeScript should not have any issue understanding this.

  • @dibbyo456
    @dibbyo456 3 года назад

    Perfect.

  • @ahmedsunil3235
    @ahmedsunil3235 4 года назад

    What is your vscode theme setting including font?

    • @clevertechie
      @clevertechie  4 года назад +3

      Material Theme Ocean, Font: Consolas, 18

  • @DoctorMGL
    @DoctorMGL Год назад +2

    still don't get it ,
    because thats just adding more size to your code while you can define the function + it functionality directly without the interface,
    i thought its like in C# where you can update something live in the UI using interface , or at least sharing the same triggered information across the app as in C++ and UE blueprint
    but in php it turn to be a joke , it just define a function name for you thats it . 0 real benefits
    its like Filling a circle with the same color twice, while the first " Fill " was enough

    • @ripplesr5655
      @ripplesr5655 Год назад

      Exactly that is what I am thinking. What is the point tho?

    • @ripplesr5655
      @ripplesr5655 Год назад

      Ok, got it. Basically this helpls you in the development phase by forcing you to adhere to a set of rules that implements the interface. Anyone, coming from TypeScript should not have any issue understanding this.

    • @returnMarcco
      @returnMarcco Год назад +3

      For example, say you have 10 classes implementing the same interface, and the interface has one method called echo_text(). You could loop over the 10 instances of those classes and call the echo_text() method on each of the 10 objects and be sure that method exists, without even knowing the implementation of the method.

  • @ArnVenture12
    @ArnVenture12 4 года назад

    Wow. Been a while???

  • @ellmatic
    @ellmatic 8 месяцев назад

    weird, i've never seen anyone use spaces in their class filenames

  • @NoAdsMurad-t3t
    @NoAdsMurad-t3t 2 месяца назад

    When you are creating an object to the actual classes, then why you need an Interface? I mean the interface looks useless here.

  • @anupkaushik9370
    @anupkaushik9370 Год назад

    You mean Abstraction and Interfaces are same thing

    • @returnMarcco
      @returnMarcco Год назад

      Polymorphism. Abstraction has to do with abstract classes

  • @mrmbeautiquethebrand
    @mrmbeautiquethebrand 2 года назад

    I don't understand. Why u are using interfaces.. Thoes work done by change class if your other class has same method you can change it your implementation class

    • @thedude9014
      @thedude9014 2 года назад +11

      I think Interfaces are used to make sure that all function that are needed are present in the class . what he missed was that if you forget to implement those function you get an error. An interface is a sort of contract and it's more about organizing code that provide a funtionality

    • @witcherle9944
      @witcherle9944 2 года назад

      @@thedude9014 your comment should be pinned

    • @Rex-lp1dl
      @Rex-lp1dl 7 месяцев назад

      @@thedude9014 You nailed it. That's more precise