DEV Community

Discussion on: Using Interfaces to Write Better PHP Code

Collapse
 
themaverickcoder1 profile image
theMaverickCoder

I have always been haunted by concepts like Interface, Abstract Classes, Design Patterns in oops cause even if I find the examples on some website, it's always has been difficult to know how to implement. Until I read this. Kudos to u for explaining this in such an easy way. I would request you to write an article for abstract classes & when to use what ( Interface or Abstract Class ).
ThanksπŸ™

Collapse
 
shinprog profile image
ShinProg (Logan Tann) • Edited

Abstraction is one of the hardest chapter of my 1st year IT studies. If you're learning self taught and only in php, it's more difficult. Still, getting the right architecture relies on understanding the basics and a lot of practice of course.

About when to use abstract and interface, there is the common pattern during my studies where you create first the interface and use it like explained in this article, then the abstract class that implements the interface (if you feel like you need it though). Your classes can extends the abstract class, whereas others ppl working in your project that might not want to hinerit from your abstract class can just rather implements the interface.

Because in java (and I think it's the case with php) you can inherit from only one class. If someone wants to add a feature he would be forced to extends the abstract class, and that would not be the case using an interface + abstract that implements it (bc he can choose to just implements the interface + extends another class and the code will run just fine)

Collapse
 
themaverickcoder1 profile image
theMaverickCoder

Hey, thanks for the brief explaination. Can u suggest any particular Java OOPS book which illustrates all these concepts with examples & practices. Will really appreciate it!

Collapse
 
ashallendesign profile image
Ash Allen

Hey! Thanks, I appreciate you saying that, it means a lot. I'm really glad that the article's helped you out!

I did consider writing a bit more in the article about when to use abstract classes vs interfaces, but didn't know if it would be too much information at once. I think I'll be writing another article soon about interfaces and abstraction, so I'll make sure to include it then. But to be honest, I think ShinProg has given a perfect explanation! πŸ˜ƒ