Last active
February 9, 2019 05:18
-
-
Save JeffreyWay/334bc75a732cf8333ec6 to your computer and use it in GitHub Desktop.
Decorator PHP dummy example
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
<?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