PHP interface

Feb 12, 2016 17:03 · 105 words · 1 minute read php thought

In object-oriented programming, interface is a contract of a class. If a class wants to have this kind interface, it must implement the methods defined in the interface. In my opinion, the idea behind the interface is let users do less work when they need to switch different classes in many places. And interface helps(or forces?) you to think about abstraction layer of the classes.

Here are examples.

Before: without interface

After: with interface

In above two examples, if we introduce a C class in a new condition, without interface, we have to change file1, file2, …etc.

With interface, we only need to change Factory.php.