# 4 # S.O.L.I.D Object Oriented Design
In PHP OOP we already know about Dependency Injection, or some design pattern like Factory, singleton. And how about SOLID?
SOLID stands for
Single responsibility principle
Open closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
# 4 # Interface segregation principle
This principle is probably the simplest in terms of theory. A client should not be forced to use interfaces that it doesn’t need. Here’s an example of violation of this principle:
// Interface segregation principle
interface InstrumentInterface {
public function play();
public function changeKey();
public function autoTune();
public function plugIn();
}
class Guitar implements InstrumentInterface {
public function play() {..}
public function changeKey() {..}
public function autoTune() {..}
public function plugIn() {..}
}
class Trumpet implements InstrumentInterface {
public function play() {..}
public function changeKey() {..}
public function autoTune() { /* Exception */ }
public function plugIn() { /* Exception */ }
}
The Trumpet class has autoTune() and plugIn() forced upon it - I’m not saying that there’s no plugin-in trumpets out there but in this case I’m talking about a standard acoustic trumpet. An ISP-safe method is to create an interface for each of the instruments that employs only the methods needed for the client:
// Interface segregation principle
interface InstrumentInterface
{
public function play();
public function changeKey();
}
interface GuitarInterface
{
public function autoTune();
public function plugIn();
}
class Guitar implements InstrumentInterface, GuitarInterface
{
public function play() {..}
public function changeKey() {..}
public function autoTune() {..}
public function plugIn() {..}
}
class Trumpet implements InstrumentInterface {
public function play() {..}
public function changeKey() {..}
}