PHP OOP Tutorial | What is a PHP interface

Поділитися
Вставка
  • Опубліковано 3 лис 2024

КОМЕНТАРІ • 131

  • @ifahddev1034
    @ifahddev1034 5 років тому +7

    I watched much tutorial for interface multi languages .. and i never understand why be should to use interface expect your tutorial I understand now, thank you so much ❤️

    • @howtocodewell
      @howtocodewell  5 років тому

      That's great to hear. I've got loads of programming tutorials on a bunch of languages so stick around. Please ask of you have any questions

  • @contabilidade00imperiocont95
    @contabilidade00imperiocont95 6 років тому +10

    Ok, this makes me look stupid but the truth is that I am a PHP developer for over a decade and never understood the purpose of interfaces. Yes, I have read a dozen of tutorials and stuff but yeah, it never broke into my skull.
    Your payment example just did the click in my brain. It further consolidated when you said at the end that there is no need for a bunch of if statements.
    Thank you mate!!!!

    • @howtocodewell
      @howtocodewell  6 років тому +1

      Hey I'm glad I could help. This is the reason I do these videos. It took a long time for me to understand OOP interfaces and it didn't instantly 'click' until I tried many examples. Now I use them on a regular basis.
      If you have any questions or suggestions for future videos then let me know.

    • @sageerahamed4447
      @sageerahamed4447 5 років тому

      Contabilidade 00 Império Contábil

  • @vineethmathews
    @vineethmathews 5 років тому +2

    Thank You Peter Fisher, I do very rarely subscribe to channels. But this video forced me to subscribe to your channel because this explained me in a simple and practical way of using interfaces in the real world environment. Thank you Jesus for leading me to this beautiful tutorial. God bless you. :-)

  • @HaraldEngels
    @HaraldEngels 4 роки тому +1

    I have watched many of these - but this is the best video about interfaces in PHP.

  • @netxyz00
    @netxyz00 4 роки тому +1

    Excellent example and explanation!

  • @mackynyxz
    @mackynyxz 4 роки тому +1

    Nice tutorial! very straight forward in explaining Interface!

  • @rimantasdanilevicius6754
    @rimantasdanilevicius6754 3 роки тому +1

    Thats was great tutorial. It made better understanding about interface in OOP. I wish to see more such clear and extended with good examples tutorial. What I missed is just how to organize folders and files, because I have been taught that Class file should have only class, and in this example it seems too many classes, so it makes a little bit confusing.
    Great teaching approach.

    • @howtocodewell
      @howtocodewell  3 роки тому +1

      Thank you very much. I’m part of the Live Coders team on Twitch and do a lot of these longer examples. I publish these to the Archive channel
      ua-cam.com/users/HowToCodeWellArchive you may find some helpful examples there

    • @rimantasdanilevicius6754
      @rimantasdanilevicius6754 3 роки тому

      ​@@howtocodewell Thank you for additional info. I saw that you are on youtube already 8 years and I will definitely will check more your videos. By the way, maybe you have somewhere tutorial how to update old version php to php8. I use Apache/Xampp, windows. I find some tutorials on youtube, but reading some comments it seems some people get errors.
      Thank you again for your time and lessons : )

  • @mrcheesee5336
    @mrcheesee5336 2 роки тому

    Very good explained - thank you!

  • @syafiq55555
    @syafiq55555 5 років тому

    You actually helped me a lot in understanding not only interface.. But the true purpose of it which focusses on cohesion and coupling. I knew a good principle of design is to have high cohesion, low coupling.. but still don’t know how to implement it in a system... thank you for your explanation. By doing this way, it will be easier to modify the system in the future. Better lecturer than my university’s lol

    • @howtocodewell
      @howtocodewell  5 років тому

      This is exactly why I do these tutorials. Thank you so much for your kind words. This inspires me to do more tutorials and courses

  • @CodeLabArtisan
    @CodeLabArtisan 4 роки тому +1

    Very much useful and helpful great video

  • @SnGrg
    @SnGrg 4 роки тому +1

    Great lesson, though 1 confusion.
    Where you implemented the third interface just to implement the process(), so that fraudCheck() could be housed, would it not have been easier to include fraudcheck() inside pay() function for MintFee class
    Like
    public function pay() {
    $this->fraudcheck();
    }
    thanks.

  • @lawrencerosen7113
    @lawrencerosen7113 4 роки тому +1

    I have tried to understand bits of "oop" whenever I came accross them. Most high level presentations left me with "When would I ever want THAT?".
    This particular film took me to the point I was really interested in.
    It just remains to say "Thanks for publishing it"

    • @howtocodewell
      @howtocodewell  4 роки тому

      Thanks Lawrence! I'm glad you found it useful

    • @vijayanpn446
      @vijayanpn446 4 роки тому

      Hii.. I have a doubt on 16th line.. why we add circle on "public function addshape(circle $shape){}" ?
      If i remove that circle it will also work without issues...

    • @lawrencerosen7113
      @lawrencerosen7113 4 роки тому +1

      @@vijayanpn446 Imagine this -
      We all have to pay bills and so I have a whole development team to handle this. They tell me they can interface to EVERYONE I need to pay.
      I send them off to write the servers and produce a single version of a client that contacts them all in the same way.
      For example, my generic interface accepts user-questions so I will call it "ItemToPay". Here are some examples:
      * Who are you?
      * What is my payment history?
      * When was last payment due?
      * When was it paid?
      * Did it go through?
      The whole project succeeded, so for the past two years, "Bill Explorer" takes many (pre-tested) servers such as "gas", "telephone" and "newspapers ".
      Now along comes a "seriously expensive" Hardware Shop. Do I rewrite the "Bill Explorer"?
      No, someone finds out to talk to the Hardware Shop, so that they fully support the same "ItemToPay".
      Once that works, there will be new instances of "ItemToPay" that I can provide to my client and NOT rewrite anything else!

  • @Cellenta
    @Cellenta 7 років тому +2

    It helped me now to understand the reason why to use interfaces. Thank you! :)

    • @howtocodewell
      @howtocodewell  7 років тому

      Thats great. I'm glad it helped. Is there anything that you would like me to cover in future tutorials?

  • @BakiYuku
    @BakiYuku 3 роки тому +1

    What is the theme you are using for your IDE? The coloring looks kind of neat and clean:)

    • @howtocodewell
      @howtocodewell  3 роки тому

      This is a dark theme in phpStorm. I think it’s called Dracula 🧛‍♂️

  • @realQkstar
    @realQkstar 5 років тому +4

    2:52 the genuine way is always The English way.❤

  • @solomonnwezeD
    @solomonnwezeD 5 років тому +2

    What I captured very well in this tutorial is that interfaces are mainly setting rules for your classes, but from the code you wrote about the payment gateway, don't you think I can decide not to use interface as I will write all the methods that is needed for the different classes and call the methods inside the process method for each of the classes. I will now call the process method for any of the classes in the paymentGateway Class because they already all have process

    • @solomonnwezeD
      @solomonnwezeD 5 років тому +1

      please I need a reply peter fisher, thanks

    • @howtocodewell
      @howtocodewell  5 років тому +1

      Hi Solomon, I'm not exactly sure what you mean.
      An interface defines the contract between two or more classes. An interface does not define the behaviour of the contract but defines the inputs and outputs.

    • @solomonnwezeD
      @solomonnwezeD 5 років тому +1

      @@howtocodewell good one, understood, thanks very much

    • @howtocodewell
      @howtocodewell  5 років тому

      @Solomon nweze you're most welcome.

    • @DarrellWoolfolk
      @DarrellWoolfolk 2 роки тому

      @@howtocodewell then what’s the point of a contract if your code already agrees on how it’s classes would work since they have the same name methods. The process class seems to do what the implement class would do by passing the class you want into it since you already instantiate the class beforehand? Or Am I wrong to think that way.

  • @parijke
    @parijke 6 років тому +1

    Excellent example to explain the principles.

  • @Nestumk
    @Nestumk 8 місяців тому +1

    thanks for great lesson

  • @ProgramoZ
    @ProgramoZ 6 років тому +2

    Great explanation Sir . I watched some videos about SOLID principle and if I am not wrong , the example shown in this video is Interface Segregation . Waiting for full in-depth tutorials with examples about SOLID in PHP . thanks again for this video .

    • @howtocodewell
      @howtocodewell  6 років тому +1

      Yes this does (or at least the last example does) fall into the category of ISP or the interface segregation principle.
      I didn't label it as such because it's quite an advanced topic and this was meant for beginners. I do plan to do a whole course on the solid principles at some point but first I want to lay the foundations.

    • @ProgramoZ
      @ProgramoZ 6 років тому

      yes...I will be waiting for that series .

    • @vlaellayug9937
      @vlaellayug9937 5 років тому +1

      @@howtocodewell this vid is super great. Have you published the whole course regarding the solid principles?

    • @howtocodewell
      @howtocodewell  5 років тому

      No not yet. I've been working on other projects but it's on the list of things to teach 👍

    • @vlaellayug9937
      @vlaellayug9937 5 років тому +1

      @@howtocodewell willing to wait. :) Thanks && Goodluck!

  • @JanVSt
    @JanVSt 5 років тому +1

    Thank you very much! It helped me understand interfaces.. Great tutorial!

  • @natnaelghirma2617
    @natnaelghirma2617 2 роки тому

    Thank you. This was good.

  • @wtransporter
    @wtransporter 4 роки тому +1

    I have similar situation like this. Question is: when to use constructor and accept object to do the job instead of local method? For example, instead of threeDCheck and fraudCheck to pass their objects?

    • @howtocodewell
      @howtocodewell  4 роки тому +1

      I wouldn't use the constructor to do any business logic if that's what you mean. Make the methods do the hard work.

    • @wtransporter
      @wtransporter 4 роки тому

      @@howtocodewell Thank you for answering. I agree with this. But what I mean is this: new SomeClass(ThreeDSecureCheckInterface $threedCheck), constructor accepts $this->threedChecker = $threedCheck and than we have local method doSomething(){$this->threedChecker->ThreeDCheck()} This is what I meant. Instead of example in PayFee class where we implemented Interface on 24:46. What if we have to implement threeDCheck interface in multiple classes? We repeat code? SRP, interfaces... I am trying to understand this things...

  • @kajetandziebaj6405
    @kajetandziebaj6405 6 років тому +2

    Thanks, brah. I understand interfaces now.

  • @elizavetawallace4356
    @elizavetawallace4356 5 років тому +2

    Very useful tutorial! Thanks

    • @howtocodewell
      @howtocodewell  5 років тому

      You're very welcome. Please let me know if you have any questions

  • @raincloud1898
    @raincloud1898 3 роки тому

    Thank you! Very helpful example! I'm quite new to the matter but after implementing "PaymentProcessInterface" shouldn't we get rid of the other three interfaces as I don't see any reason to use them in this case? Maybe we could declare them as normal classes and extend them in the payment type classes if needed? Many thanks for your help!

  • @Killer24nl
    @Killer24nl 5 років тому

    Excellent tutorial, thank you very much!

  • @Dani-si8ie
    @Dani-si8ie 4 роки тому +1

    Coming from Swift, this is basically Protocols. The idea is pretty much the same, the naming is obviously different, interfaces sounds very tricky. But thank you for the explanation, i watched that in other classes and yikes, didnt really get it

    • @howtocodewell
      @howtocodewell  4 роки тому

      Thank you. I've never used Swift before so I couldn't comment on the comparison

  • @kleverestcompany1800
    @kleverestcompany1800 5 років тому +2

    @Peter Fisher I wonder if is a good idea to use a trait instead of 2 more interfaces to give the extra functionality FraudCheck to the class that need it ? I would appreciate it if you reply me

    • @howtocodewell
      @howtocodewell  5 років тому +2

      Great question. An interface cannot define the method body whereas a trait can. An interface is used to define the contractual obligations between multiple classes.
      We couldn't use a trait in this case as the body of fraudCheck was different between the classes however the implementation was the same.
      This has given me a great idea for a trait Vs interface video so I can demonstrate this

  • @Juli-sp8kj
    @Juli-sp8kj 6 років тому +1

    Thanks for your explanation! Only interface tutorial I have understood :D

    • @howtocodewell
      @howtocodewell  6 років тому

      That's great to hear. Do let me know if you have suggestions for future tutorials. Happy coding

    • @Juli-sp8kj
      @Juli-sp8kj 6 років тому

      Thanks for replying! Do you have any tutorials on using OOP with databases?

    • @howtocodewell
      @howtocodewell  6 років тому

      I have course/playlist on using MYSQLi which is the PHP OOP way of accessing MYSQL: ua-cam.com/video/wA2GL0aVjPA/v-deo.html

  • @busyrand
    @busyrand 6 років тому +2

    RUN PHP!!! Awesome Mug!

    • @howtocodewell
      @howtocodewell  6 років тому

      Yep great mug. I'm drinking tea from it now!

  • @howtocodewell
    @howtocodewell  7 років тому

    Today's tutorial covers PHP interfaces! Interfaces are the very important for object oriented programming. Let me know what other OOP subjects you wish me to cover in future videos

  • @borisnaftaliev3602
    @borisnaftaliev3602 5 років тому

    Great lesson, thank you for sharing.

  • @tommyvid633481
    @tommyvid633481 6 років тому +1

    This is a great explanation. Just one thing. In the first 10 minutes with the shape example, you added ‘shape interface’ as a parameter in the addShape method. What does this do as I believe this would still work with only $shape as the argument?

    • @howtocodewell
      @howtocodewell  6 років тому

      Yes just having $shape as the parameter would work in that instance but you are losing out on DataType checking and IDE hinting. Plus You are allowing any type of shape into the method. If this class was in another file and created by another developer you would probably get confused as to what $shape represents. Interfaces make the code more readable as you know exactly how that interface should behave (What methods that class implements)

    • @tommyvid633481
      @tommyvid633481 6 років тому

      Peter Fisher that’s great. I get what you mean - it’s just like really strapping down your code and making sure it really works well to a fixed set of rules that are made flexibly by enforcing good usage of interfaces. Thanks again.

  • @skell_jams
    @skell_jams 6 років тому +1

    Really really great stuff. Do you do more of this?

    • @howtocodewell
      @howtocodewell  6 років тому

      Hi +Sean Kelly yes I sure do.
      Tuesdays I do a tutorial like this
      Friday's I talk about web development/career/freelance
      Certain Paterons get early access to videos and can join my discord server to get coding help www.patreon.com/howToCodeWell

  • @thomasvarghese744
    @thomasvarghese744 6 років тому

    Thanks for the great tutorial. There is a confusion I am having about the object you declared for the payment gateway example. Since you created an object only for mintfee and passed it how will other 2 class methods will be code. I tried to run the code you have given and only the mintfee class methods are called. Can you explain that and clear the doubt?

    • @howtocodewell
      @howtocodewell  6 років тому

      Hi Thomas, have you compared your code to what is on GitHub: github.com/pfwd/how-to-code-well-tutorials/blob/master/tutorials/php/oop-beginners/interface/example_2.php
      It should work as each Payments type has a process method.

  • @RameenFallschirmjager
    @RameenFallschirmjager 4 роки тому

    2020 and PHP is not dead and it's more alive than some other technologies! Has anyone heard anything of ruby for a while?!

  • @chrismarceljames
    @chrismarceljames 6 років тому

    I'm just seeing this video and I appreciate your simplified explanation, I just read a source on Dependency Injection and how ir eliminates tight coupling. This looks quite similar to what was implemented in the source I read. Could your example be likened to a somewhat implementation of Dependency Injection.

    • @howtocodewell
      @howtocodewell  6 років тому

      Without reading it myself I cannot say for sure. However this does help to reduce coupling of code, features and responsibilities within a system.

    • @chrismarceljames
      @chrismarceljames 6 років тому +1

      Here is the link to the post php-di.org/doc/understanding-di.html My next question is this, instead of using Interfaces, could we have used Traits? I was assuming that, since Traits also helped in solving the limitations of PHP not supporting multiple inheritance. Thank you.

    • @howtocodewell
      @howtocodewell  6 років тому

      Great question. An interface is like a contract between two people. The contract states what is required by the class to fulfill its responsibilities. A trait cannot be instantiated and nor can it implement interfaces. It is simply telling PHP to drop a set of functionality into a class. So its a question of design. Both have pros and cons.
      ... There is an RFC about traits with interfaces wiki.php.net/rfc/traits-with-interfaces

  • @rw7799
    @rw7799 6 років тому +1

    i thought duplication was a bad thing..is there a better way to put the three shapes in the same class or call them with the same attributes somehow? i like the format of code and you on same screen..alot easier to concentrate. thx

    • @howtocodewell
      @howtocodewell  6 років тому

      Duplication is a code smell but that doesn't mean the method names of each class needs to be unique. Using interfaces we can segregate our classes whilst enforcing the behaviours of those classes.

    • @rw7799
      @rw7799 6 років тому

      so do you need to name circle, square, line as seperate classes or cant you put them into 1 class if they do the same thing? tbf it doesnt really matter ha

    • @howtocodewell
      @howtocodewell  6 років тому

      In this example they can all be in a single file but you would normal have one class per file

    • @climatechangedoesntbargain9140
      @climatechangedoesntbargain9140 5 років тому

      @@howtocodewellhe asked about classes, not files -> of course, if they do the same thing, then omit the redundant classes :)

  • @raphaelandrade555
    @raphaelandrade555 6 років тому +2

    In the case of shapes, couldnt you just use an abstract inheritance with abstract functions instead of interface?

  • @llBestBoyll
    @llBestBoyll 2 роки тому

    nice video man

  • @wdseelig
    @wdseelig 6 років тому +1

    Peter, Is the code in this tutorial available on github? Searched for it and didn't find anything?

    • @howtocodewell
      @howtocodewell  6 років тому +1

      Hi yes it's here:
      github.com/pfwd/how-to-code-well-tutorials/tree/master/tutorials/php/oop-beginners/interface
      I need to think about how to link to the code better. I was thinking of having one repo per tutorial series/course but that would be a huge amount of repo's.

    • @wdseelig
      @wdseelig 6 років тому +1

      Thanks very much for this. And, since I didn't say so earlier, thanks for this tutorial. I've been looking for an explanation of interfaces for quite some time and this is by far the best example I've come across.

    • @howtocodewell
      @howtocodewell  6 років тому

      Your very welcome. If you have any suggestions for future tutorials hen let me know

  • @rw7799
    @rw7799 6 років тому +1

    where do these functions go in your code?..do you use 'includes' to call them?

    • @howtocodewell
      @howtocodewell  6 років тому +2

      You could use PHP autoloading and/or require_one

    • @rw7799
      @rw7799 6 років тому +1

      require_one 'function.php' ;
      thx for term.this explains it.
      ua-cam.com/video/VC5HM0J0_Vo/v-deo.html

  • @rw7799
    @rw7799 6 років тому

    you know you use an interface to be able to swap between different classes... wouldnt a drop down menu on the users side do the same? or is this what the interface allows you to do on the users side?

    • @howtocodewell
      @howtocodewell  6 років тому

      I'm not sure what you mean by the 'users side'?

    • @rw7799
      @rw7799 6 років тому

      i always see these tutorials as apps with a real user at tother end of the code.. so i see users side as if this was an app you were designing where a real person is using the app to create shapes.. does that make sense.
      so a drop down menu would dynamically change
      circle
      square
      line
      on select of one of these shapes the code would fire, thus taking away need for an interface

    • @howtocodewell
      @howtocodewell  6 років тому +1

      You should still use interfaces as these will enforce the behaviours of future shapes (From a code perspective). I would think more about designing the classes in OOP and less about the overall GUI.

  • @rahifirobinson9991
    @rahifirobinson9991 7 років тому

    Hi, do you know a few emerging trends respect to php interfaces?

    • @howtocodewell
      @howtocodewell  7 років тому

      Hi, How do you mean emerging trends? A PHP interface is normally used to define how other classes should integrate with the rest of the application

    • @rahifirobinson9991
      @rahifirobinson9991 7 років тому

      The question is to perform a research on any emerging trends with respect to creating PHP interfaces and use this knowledge to add luster to your interface

    • @rahifirobinson9991
      @rahifirobinson9991 7 років тому

      Can you help me with that?

    • @howtocodewell
      @howtocodewell  6 років тому

      Sorry for the delay. I'm not really sure what you mean by luster. Soon I will be creating a tutorial using PHP interfaces with Symfony services which may be of interest

    • @jasondavies3772
      @jasondavies3772 5 років тому

      Traits, its as simple as that.

  • @carlortiz5181
    @carlortiz5181 6 років тому +1

    Why don't you just put the fraud checking method inside pay method?

    • @howtocodewell
      @howtocodewell  6 років тому +1

      Fraud checking and payment are two separate processes and have their own responsibilities. Also having them separate makes testing easier.

    • @carlortiz5181
      @carlortiz5181 6 років тому +1

      Make sense. Thank you.

  • @rohitsagar9011
    @rohitsagar9011 3 роки тому +1

    😑 omg I m so confused between abstract and inteface

    • @howtocodewell
      @howtocodewell  3 роки тому

      Jump on a live stream or the Discord server and we a can discuss it.

    • @rohitsagar9011
      @rohitsagar9011 3 роки тому +1

      @@howtocodewell link ?

  • @RameenFallschirmjager
    @RameenFallschirmjager 4 роки тому

    You were confused about this yourself, it's obvious from your hesitant explanation, not very good.

  • @climatechangedoesntbargain9140
    @climatechangedoesntbargain9140 5 років тому

    oh dear - php is just awfull.

    • @howtocodewell
      @howtocodewell  5 років тому

      Why do you say that?

    • @climatechangedoesntbargain9140
      @climatechangedoesntbargain9140 5 років тому

      ​@@howtocodewellmainly bc. of the lack of typing, as soon as you hit array like structures you don't even have a chance despite IDE and PHP 7.
      Also php lacks a lot of useful features.
      The community seems to have shrunk.

    • @howtocodewell
      @howtocodewell  5 років тому

      Can I ask where you are from? In the UK the PHP community is still buzzing. Perhaps it's different in other countries.
      I'm a Python and a PHP dev (I also dip my toes into other/more strictly typed languages from time to time)
      Tuples and lists can also be frustrating to use at times. Especially if they are not cleanly defined. But I best what you are saying about PHP arrays.

    • @climatechangedoesntbargain9140
      @climatechangedoesntbargain9140 5 років тому +1

      @@howtocodewellI am from Germany - but I was not talking about the local community, but the online one. I feel like there is really little content about php later than 5 or modern concepts in general.
      I switched from arrays to github.com/php-ds/ext-ds ;) (Unfortunately still not typed, but without generics and code generation this is probably the best one can get)
      And I come mainly from C++, C#, Go, TypeScript (in descending order)

    • @howtocodewell
      @howtocodewell  5 років тому

      I've never heard or used that extension. I may check it out. Thanks.