Skip to content

@dustins /gist:1623410
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
<?php
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.