SOLID/2-open-closed-principle.php

63 lines
897 B
PHP
Raw Normal View History

2016-04-20 13:01:31 +00:00
<?php
// 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();
}
}