r/PHPhelp • u/[deleted] • Nov 17 '22
Solved PHP Abstract Classes, PHP Interfaces
I read about it online and still can't understand what these two is about. How does it work and when do you use these two? Can someone give example and explain these to me in simple words? Thanks!
8
Upvotes
2
u/stfcfanhazz Nov 17 '22
Abstract classes are useful when every implementation of them (every sub-class) shares some common behaviour. It saves you having to code the same or similar logic in every implementation. Interfaces define what methods a class needs to have, and it means in dependent code where you don't care what kind of implementation you get, you can type hint for the interface and you know what methods are available.