Skip to content

Instantly share code, notes, and snippets.

@ichiriac
Created May 11, 2015 12:32
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 ichiriac/e5dd520a0be57ca163f7 to your computer and use it in GitHub Desktop.
Save ichiriac/e5dd520a0be57ca163f7 to your computer and use it in GitHub Desktop.
decorator pattern
<?php
interface Decorable {
public function doSomething();
}
interface Decorator extends Decorable {
public function __construct(Decorable $target);
}
class Something implements Decorable {
public function doSomething() {
return true;
}
}
class Somehow implements Decorator {
protected $target;
public function __construct(Decorable $target) {
$this->target = $target;
}
public function doSomething() {
// do some stuff before or after calling the method
return $this->target->doSomething();
}
}
// RUN ...
$foo = new Something();
$bar = new Somehow($foo);
$bar->doSomething();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment