Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
interface DecoratorInterface {
public function decorate();
trait DecoratorTrait {
protected $decorators = array();
//public function addDecorator(DecoratorInterface $decorator)...
public function decorate($input)
$output = $input;
foreach ($this->decorators as $decorator) {
$output = $decorator->decorate($output);
return sprintf('Decorated String: %s', $output);
public function __toString()
return $this->decorate(parent::__toString());
class SomeObject {
protected $someText;
public function __construct($someText = '')
$this->someText = $someText;
public function __toString()
return $this->someText;
class DecoratedObject extends SomeObject {
use DecoratorTrait;
// maybe do other stuff
$object1 = new SomeObject('foo');
echo $object1 . PHP_EOL; // foo
$object = new DecoratedObject('foo');
echo $object . PHP_EOL; // Decorated String: foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.