-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Php examples #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Alienpruts
wants to merge
28
commits into
kamranahmedse:master
Choose a base branch
from
Alienpruts:php-examples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Php examples #66
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
10fc66b
Added SimpleFactory example
369e83a
Added FactoryMethod example
c1ebc44
Added Abstract Factory example
1c9423f
Added Builder example
2c1f113
Added Prototype example
e85ad07
Added Singleton example
6062aa2
Added Adapter example
6fa88a8
Added Bridge example
f590f14
Added Composite example
316fcd2
Added Decorator example
444b2f5
Added Facade example
404217e
Added Flyweight example, added own implementation to demonstrate the …
7ed5d2d
Added Proxy example, with one small addition
ca96959
Small syntax changes + PSR-2 compliance
b777cc1
Added Chain Of Command example
0cd70fe
Added Command example
a52e9ca
Added Iterator example, slightly modified to better show the workings
f7c1310
Small typo fix in Iterator example
47c5081
Removed .gitignore as per suggestion of @povils
c56150b
Added newlines at the end of files, per request @povils
e1f470a
Switched echo command with the actual action, per request @povils
2cf6864
Added Mediator example
27d9a76
Added Memento example
b9b1fa6
Added Observer example
1eb2fab
Added Visitor example
4190967
Added Strategy example
668d73d
Added State example
ce83c6a
Added Template example
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
examples-per-language/php/behavioral/Chain_Of_Responsibility.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
use designPatternsForHumans\behavioral\Chain_Of_Responsibility\Bank; | ||
use designPatternsForHumans\behavioral\Chain_Of_Responsibility\BitCoin; | ||
use designPatternsForHumans\behavioral\Chain_Of_Responsibility\Paypal; | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
// Let's prepare a chain like below | ||
// $bank->$paypal->$bitcoin | ||
|
||
$bank = new Bank(100); | ||
$paypal = new Paypal(200); | ||
$bitcoin = new BitCoin(300); | ||
|
||
// First priority bank | ||
// If bank can't pay then paypal | ||
// If paypal can't pay then bit coin. | ||
$bank->setNext($paypal); | ||
$paypal->setNext($bitcoin); | ||
|
||
// Let's try and pay our bill. | ||
$bank->pay(259); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
use designPatternsForHumans\behavioral\Command\Bulb; | ||
use designPatternsForHumans\behavioral\Command\RemoteControl; | ||
use designPatternsForHumans\behavioral\Command\TurnOff; | ||
use designPatternsForHumans\behavioral\Command\TurnOn; | ||
|
||
$bulb = new Bulb(); | ||
|
||
$turnOn = new TurnOn($bulb); | ||
$turnOff = new TurnOff($bulb); | ||
|
||
$remote = new RemoteControl(); | ||
$remote->submit($turnOn); | ||
$remote->submit($turnOff); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
use designPatternsForHumans\behavioral\Iterator\RadioStation; | ||
use designPatternsForHumans\behavioral\Iterator\StationList; | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
$stationList = new StationList(); | ||
|
||
$stationList->addStation(new RadioStation(89)); | ||
$stationList->addStation(new RadioStation(101)); | ||
$stationList->addStation(new RadioStation(102)); | ||
$stationList->addStation(new RadioStation(103.2)); | ||
|
||
/** @var RadioStation $station */ | ||
echo 'These are the known Radio Stations' . PHP_EOL; | ||
foreach ($stationList as $station) { | ||
echo $station->getFrequency(). PHP_EOL; | ||
} | ||
|
||
$stationList->removeStation(new RadioStation(89)); | ||
|
||
// To prove the station was indeed removed, we iterate through the list again. | ||
// Small caveat : Iterators need to be rewinded after beeing Iterated! | ||
$stationList->rewind(); | ||
|
||
/** @var RadioStation $station */ | ||
echo 'These are the known Radio Stations' . PHP_EOL; | ||
foreach ($stationList as $station) { | ||
echo $station->getFrequency(). PHP_EOL; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
use designPatternsForHumans\behavioral\Mediator\ChatRoom; | ||
use designPatternsForHumans\behavioral\Mediator\User; | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
$mediator = new ChatRoom(); | ||
|
||
$john = new User('John Doe', $mediator); | ||
$jane = new User('Jane Doe', $mediator); | ||
|
||
$john->send('Hi there!'); | ||
$jane->send('Hey!'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use designPatternsForHumans\behavioral\Memento\Editor; | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
$editor = new Editor(); | ||
|
||
$editor->type('This is the first sentence' . PHP_EOL); | ||
$editor->type('This is the second sentence' . PHP_EOL); | ||
|
||
$saved = $editor->save(); | ||
|
||
$editor->type('And this is the third one.' . PHP_EOL); | ||
|
||
echo $editor->getContent(); | ||
|
||
$editor->restore($saved); | ||
echo 'Restored $editor.' . PHP_EOL; | ||
echo $editor->getContent(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
use designPatternsForHumans\behavioral\Observer\JobPost; | ||
use designPatternsForHumans\behavioral\Observer\JobPostings; | ||
use designPatternsForHumans\behavioral\Observer\JobSeeker; | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
$john = new JobSeeker('John Doe'); | ||
$jane = new JobSeeker('Jane Doe'); | ||
|
||
$jobPostings = new JobPostings(); | ||
$jobPostings->attach($john); | ||
$jobPostings->attach($jane); | ||
|
||
$jobPostings->add(new JobPost('Software Engineer')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use designPatternsForHumans\behavioral\State\DefaultState; | ||
use designPatternsForHumans\behavioral\State\LowerCase; | ||
use designPatternsForHumans\behavioral\State\TextEditor; | ||
use designPatternsForHumans\behavioral\State\UpperCase; | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
$editor = new TextEditor(new DefaultState()); | ||
|
||
$editor->type('First line'); | ||
|
||
$editor->setState(new UpperCase()); | ||
|
||
$editor->type('Second line'); | ||
$editor->type('Third line'); | ||
|
||
$editor->setState(new LowerCase()); | ||
|
||
$editor->type('Fourth line'); | ||
$editor->type('Fifth line'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
use designPatternsForHumans\behavioral\Strategy\BubbleSortStrategy; | ||
use designPatternsForHumans\behavioral\Strategy\QuickSortStrategy; | ||
use designPatternsForHumans\behavioral\Strategy\Sorter; | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
$dataset = [1, 5, 2, 6, 3]; | ||
|
||
$sorter = new Sorter(new BubbleSortStrategy()); | ||
$sorter->sort($dataset); | ||
|
||
$sorter = new Sorter(new QuickSortStrategy()); | ||
$sorter->sort($dataset); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
use designPatternsForHumans\behavioral\Template\AndroidBuilder; | ||
use designPatternsForHumans\behavioral\Template\IosBuilder; | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
$androidBuilder = new AndroidBuilder(); | ||
$androidBuilder->build(); | ||
|
||
$iosBuilder = new IosBuilder(); | ||
$iosBuilder->build(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
use designPatternsForHumans\behavioral\Visitor\Dolphin; | ||
use designPatternsForHumans\behavioral\Visitor\Jump; | ||
use designPatternsForHumans\behavioral\Visitor\Lion; | ||
use designPatternsForHumans\behavioral\Visitor\Monkey; | ||
use designPatternsForHumans\behavioral\Visitor\Speak; | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
$monkey = new Monkey(); | ||
$lion = new Lion(); | ||
$dolphin = new Dolphin(); | ||
|
||
// This is the visitor. | ||
$speak = new Speak(); | ||
|
||
$monkey->accept($speak); | ||
$lion->accept($speak); | ||
$dolphin->accept($speak); | ||
|
||
// Using the Visitor pattern it becomes easy to 'add' new functionality to | ||
// the animals. Declare functionality in a class that implements Visitor. | ||
|
||
$jump = new Jump(); | ||
|
||
$monkey->accept($jump); | ||
$lion->accept($jump); | ||
$dolphin->accept($jump); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
spl_autoload_register(function($className) { | ||
$explode = explode("\\", $className); | ||
$slice = array_slice($explode, -2, 2); | ||
require_once 'src' . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $slice) . '.php'; | ||
}); |
34 changes: 34 additions & 0 deletions
34
examples-per-language/php/behavioral/src/Chain_Of_Responsibility/Account.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility; | ||
|
||
abstract class Account | ||
{ | ||
protected $balance; | ||
/** @var Account */ | ||
protected $successor; | ||
|
||
public function setNext(Account $account) | ||
{ | ||
$this->successor = $account; | ||
} | ||
|
||
public function pay($amountToPay) | ||
{ | ||
if ($this->canPay($amountToPay)) { | ||
echo sprintf('Paid %s using %s' . PHP_EOL, $amountToPay, | ||
get_called_class()); | ||
} elseif ($this->successor) { | ||
echo sprintf('Cannot pay using %s. Proceeding ...' . PHP_EOL, | ||
get_called_class()); | ||
$this->successor->pay($amountToPay); | ||
} else { | ||
throw new \Exception('None of the accounts have enough balance'); | ||
} | ||
} | ||
|
||
public function canPay($amount) | ||
{ | ||
return $this->balance >= $amount; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
examples-per-language/php/behavioral/src/Chain_Of_Responsibility/Bank.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility; | ||
|
||
|
||
class Bank extends Account | ||
{ | ||
// In the example the balance property is redefined, but that is not | ||
// necessary, since we extend Account, which has that property. | ||
|
||
|
||
public function __construct($balance) | ||
{ | ||
$this->balance = $balance; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
examples-per-language/php/behavioral/src/Chain_Of_Responsibility/BitCoin.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility; | ||
|
||
|
||
class BitCoin extends Account | ||
{ | ||
// In the example the balance property is redefined, but that is not | ||
// necessary, since we extend Account, which has that property. | ||
|
||
|
||
public function __construct($balance) | ||
{ | ||
$this->balance = $balance; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
examples-per-language/php/behavioral/src/Chain_Of_Responsibility/Paypal.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility; | ||
|
||
|
||
class Paypal extends Account | ||
{ | ||
// In the example the balance property is redefined, but that is not | ||
// necessary, since we extend Account, which has that property. | ||
|
||
|
||
public function __construct($balance) | ||
{ | ||
$this->balance = $balance; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Command; | ||
|
||
|
||
// This is the receiver, it has the implementation of the actions we want to perform. | ||
class Bulb | ||
{ | ||
public function turnOn() | ||
{ | ||
echo 'Bulb has been lit!' . PHP_EOL; | ||
} | ||
|
||
public function turnOff() | ||
{ | ||
echo 'Darkness!' . PHP_EOL; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
examples-per-language/php/behavioral/src/Command/Command.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Command; | ||
|
||
|
||
interface Command | ||
{ | ||
public function execute(); | ||
public function undo(); | ||
public function redo(); | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
examples-per-language/php/behavioral/src/Command/RemoteControl.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Command; | ||
|
||
// This is the invoker, this will be used by client to interact. | ||
class RemoteControl | ||
{ | ||
public function submit(Command $command) | ||
{ | ||
$command->execute(); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
examples-per-language/php/behavioral/src/Command/TurnOff.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Command; | ||
|
||
|
||
class TurnOff implements Command | ||
{ | ||
|
||
protected $bulb; | ||
|
||
public function __construct(Bulb $bulb) | ||
{ | ||
$this->bulb = $bulb; | ||
} | ||
|
||
public function execute() | ||
{ | ||
$this->bulb->turnOff(); | ||
} | ||
|
||
public function undo() | ||
{ | ||
$this->bulb->turnOn(); | ||
} | ||
|
||
public function redo() | ||
{ | ||
$this->execute(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use composer to set psr-4 and avoid put this line everywhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is true, but as far as I'm concerned, I tried to make these examples as such that they are not dependent on Composer or any such 'mechanism', hence why I wrote my own little autoloader.
I might rework this to only have one autoload.php, but at the time I was thinking that maybe people wanted to test one set of examples, that is why I used the require_once on each of the examples.