mirror of
https://github.com/wataridori/solid-php-example.git
synced 2024-12-04 19:03:30 +00:00
63 lines
897 B
PHP
63 lines
897 B
PHP
|
<?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();
|
||
|
}
|
||
|
}
|