mirror of
https://github.com/wataridori/solid-php-example.git
synced 2024-12-04 02:48:26 +00:00
Init
This commit is contained in:
commit
c509e66670
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.idea
|
||||
.DS_Store
|
63
1-single-responsibility-principle.php
Normal file
63
1-single-responsibility-principle.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
// Single Responsibility Principle Violation
|
||||
class Report
|
||||
{
|
||||
public function getTitle()
|
||||
{
|
||||
return 'Report Title';
|
||||
}
|
||||
|
||||
public function getDate()
|
||||
{
|
||||
return '2016-04-21';
|
||||
}
|
||||
|
||||
public function getContents()
|
||||
{
|
||||
return [
|
||||
'title' => $this->getTitle(),
|
||||
'date' => $this->getDate(),
|
||||
];
|
||||
}
|
||||
|
||||
public function formatJson()
|
||||
{
|
||||
return json_encode($this->getContents());
|
||||
}
|
||||
}
|
||||
|
||||
// Refactored
|
||||
class Report
|
||||
{
|
||||
public function getTitle()
|
||||
{
|
||||
return 'Report Title';
|
||||
}
|
||||
|
||||
public function getDate()
|
||||
{
|
||||
return '2016-04-21';
|
||||
}
|
||||
|
||||
public function getContents()
|
||||
{
|
||||
return [
|
||||
'title' => $this->getTitle(),
|
||||
'date' => $this->getDate(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
interface ReportFormattable
|
||||
{
|
||||
public function format(Report $report);
|
||||
}
|
||||
|
||||
class JsonReportFormatter implements ReportFormattable
|
||||
{
|
||||
public function format(Report $report)
|
||||
{
|
||||
return json_encode($report->getContents());
|
||||
}
|
||||
}
|
62
2-open-closed-principle.php
Normal file
62
2-open-closed-principle.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
66
3-liskov-substitution-principle.php
Normal file
66
3-liskov-substitution-principle.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
// Liskov Substitution Principle Violation
|
||||
// The Rectangle - Square problem
|
||||
class Rectangle
|
||||
{
|
||||
protected $width;
|
||||
protected $height;
|
||||
|
||||
public function setHeight($height)
|
||||
{
|
||||
$this->height = $height;
|
||||
}
|
||||
|
||||
public function getHeight()
|
||||
{
|
||||
return $this->height;
|
||||
}
|
||||
|
||||
public function setWidth($width)
|
||||
{
|
||||
$this->width = $width;
|
||||
}
|
||||
|
||||
public function getWidth()
|
||||
{
|
||||
return $this->width;
|
||||
}
|
||||
|
||||
public function area()
|
||||
{
|
||||
return $this->height * $this->width;
|
||||
}
|
||||
}
|
||||
|
||||
class Square extends Rectangle
|
||||
{
|
||||
public function setHeight($value)
|
||||
{
|
||||
$this->width = $value;
|
||||
$this->height = $value;
|
||||
}
|
||||
|
||||
public function setWidth($value)
|
||||
{
|
||||
$this->width = $value;
|
||||
$this->height = $value;
|
||||
}
|
||||
}
|
||||
|
||||
class RectangleTest
|
||||
{
|
||||
private $rectangle;
|
||||
|
||||
public function __construct(Rectangle $rectangle)
|
||||
{
|
||||
$this->rectangle = $rectangle;
|
||||
}
|
||||
|
||||
public function testArea()
|
||||
{
|
||||
$this->rectangle->setHeight(2);
|
||||
$this->rectangle->setWidth(3);
|
||||
// Expect rectangle's area to be 6
|
||||
}
|
||||
}
|
94
4-interface-segregation-principle.php
Normal file
94
4-interface-segregation-principle.php
Normal file
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
// Interface Segregation Principle Violation
|
||||
interface Workable
|
||||
{
|
||||
public function code();
|
||||
public function test();
|
||||
}
|
||||
|
||||
class Programmer implements Workable
|
||||
{
|
||||
public function canCode()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function code()
|
||||
{
|
||||
return 'coding';
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
return 'testing in localhost';
|
||||
}
|
||||
}
|
||||
|
||||
class Tester implements Workable
|
||||
{
|
||||
public function canCode()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function code()
|
||||
{
|
||||
throw new Exception('Opps! I can not code');
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
return 'testing in test server';
|
||||
}
|
||||
}
|
||||
|
||||
class ProjectManagement
|
||||
{
|
||||
public function processCode(Workable $member)
|
||||
{
|
||||
if ($member->canCode()) {
|
||||
$member->code();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Refactored
|
||||
interface Codeable
|
||||
{
|
||||
public function code();
|
||||
}
|
||||
|
||||
interface Testable
|
||||
{
|
||||
public function test();
|
||||
}
|
||||
|
||||
class Programmer implements Codeable, Testable
|
||||
{
|
||||
public function code()
|
||||
{
|
||||
return 'coding';
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
return 'testing in localhost';
|
||||
}
|
||||
}
|
||||
|
||||
class Tester implements Workable
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
return 'testing in test server';
|
||||
}
|
||||
}
|
||||
|
||||
class ProjectManagement
|
||||
{
|
||||
public function processCode(Codeable $member)
|
||||
{
|
||||
$member->code();
|
||||
}
|
||||
}
|
48
5-dependency-inversion-principle.php
Normal file
48
5-dependency-inversion-principle.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
// Dependency Inversion Principle Violation
|
||||
class Mailer
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class SendWelcomeMessage
|
||||
{
|
||||
private $mailer;
|
||||
|
||||
public function __construct(Mailer $mailer)
|
||||
{
|
||||
$this->mailer = $mailer;
|
||||
}
|
||||
}
|
||||
|
||||
// Refactored
|
||||
interface Mailer
|
||||
{
|
||||
public function send();
|
||||
}
|
||||
|
||||
class SmtpMailer implements Mailer
|
||||
{
|
||||
public function send()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class SendGridMailer implements Mailer
|
||||
{
|
||||
public function send()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class SendWelcomeMessage
|
||||
{
|
||||
private $mailer;
|
||||
|
||||
public function __construct(Mailer $mailer)
|
||||
{
|
||||
$this->mailer = $mailer;
|
||||
}
|
||||
}
|
18
readme.md
Normal file
18
readme.md
Normal file
@ -0,0 +1,18 @@
|
||||
## SOLID Principles Examples in PHP
|
||||
|
||||
### Single Responsibility Principle
|
||||
A class should have one, and only one, reason to change. [Example](./1-single-responsibility-principle.php)
|
||||
|
||||
### Open Closed Principle
|
||||
A class should be open for extension, but closed for modification. [Example](./2-open-closed-principle.php)
|
||||
|
||||
### Liskov Substitution Principle
|
||||
Derived classes must be substitutable for their base classes. [Example](./3-liskov-substitution-principle.php)
|
||||
|
||||
### Interface Segregation Principle
|
||||
Many client-specific interfaces are better than one general-purpose interface. [Example](./4-interface-segregation-principle.php)
|
||||
|
||||
### Dependency Inversion Principle
|
||||
Depend upon abstractions. Do not depend upon concretions. [Example](./5-dependency-inversion-principle.php)
|
||||
|
||||
#### For more information about *Object Oriented Design Principles*, you can refer [this slide](https://viblo.asia/thangtd90/posts/pVYRPJPmG4ng)
|
Loading…
Reference in New Issue
Block a user