Skip to content

Instantly share code, notes, and snippets.

@hopeseekr
Created March 13, 2019 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopeseekr/f819390c6178f896d0e40e4f2330fe5d to your computer and use it in GitHub Desktop.
Save hopeseekr/f819390c6178f896d0e40e4f2330fe5d to your computer and use it in GitHub Desktop.
Strategy Pattern Educational Code
<?php
interface RESTAuthDriver
{
public function setApiClient(RESTSpeaker $apiClient);
public function generateGuzzleAuthOptions(): array;
}
interface Foo2
{
public function hi();
}
abstract class Foo
{
// public function hi() { echo "Hi\n"; }
abstract protected function hi();
public function greet()
{
echo $this->hi();
}
}
$realFoo = new class extends Foo
{
protected function hi()
{
echo "Foo\n";
}
};
class Bar extends Foo
{
protected function hi()
{
echo "Bar\n";
}
}
$hiStrategy = 'BarZ';
(new $hiStrategy)->greet();
$realFoo->greet();
(new Bar())->greet();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment