DEV Community

Discussion on: Using Interfaces to Write Better PHP Code

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!