Skip to content

Instantly share code, notes, and snippets.

@gghughunishvili
Forked from JeffreyWay/decorator.php
Created June 12, 2017 08:04
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 gghughunishvili/9d373a1af6270e4405e766f72a8c13c2 to your computer and use it in GitHub Desktop.
Save gghughunishvili/9d373a1af6270e4405e766f72a8c13c2 to your computer and use it in GitHub Desktop.
Decorator PHP dummy example
<?php
interface Thing {
public function execute();
}
class A implements Thing {
public function execute()
{
var_dump('Core stuff is executing');
}
}
class B implements Thing {
public function __construct(Thing $thing)
{
$this->thing = $thing;
}
public function execute()
{
var_dump('class b is doing something');
$this->thing->execute();
}
}
class C implements Thing {
public function __construct(Thing $thing)
{
$this->thing = $thing;
}
public function execute()
{
var_dump('class c is doing something');
$this->thing->execute();
}
}
(new B(new C(new A)))->execute();
/*
"class b is doing something"
"class c is doing something"
"Core stuff is executing"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment