# 2 # 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
# 2 # Open closed principle
A clever application design and the code writing part should take care of the frequent changes that are done during the development and the maintaining phase of an application. Usually, many changes are involved when a new functionality is added to an application. Those changes in the existing code should be minimized, since it's assumed that the existing code is already unit tested and changes in already written code might affect the existing functionality in an unwanted manner.
The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged.
// Open Closed Principle Violation
class Programmer
{
public function code()
{
return 'coding';
}
}
class Tester
{
public function test()
{
return 'testing';
}
}
class ProjectManagement
{
public function process($member)
{
if ($member instanceof Programmer) {
$member->code();
} elseif ($member instanceof Tester) {
$member->test();
};
throw new Exception('Invalid input member');
}
}
// Refactored
interface Workable
{
public function work();
}
class Programmer implements Workable
{
public function work()
{
return 'coding';
}
}
class Tester implements Workable
{
public function work()
{
return 'testing';
}
}
class ProjectManagement
{
public function process(Workable $member)
{
return $member->work();
}
}