r/PHPhelp • • 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

9 comments sorted by

View all comments

2

u/bkdotcom Nov 17 '22

an interface is akin to a "contract"..
If a class implements an interface, you know it contains the interface's methods. You can typehint and check against the interface vs some specific implementation

function __construct(SomeInterface $dependancy)

An abstract is simply a base class / building block that must be extended by a non-abstract class to be usable.. Good for defining base functionality...