Skip to content

Instantly share code, notes, and snippets.

@stefankleff
Created July 4, 2014 13:32
Show Gist options
  • Save stefankleff/f6d16a882fabe8c19dc8 to your computer and use it in GitHub Desktop.
Save stefankleff/f6d16a882fabe8c19dc8 to your computer and use it in GitHub Desktop.
Escaping with a proxy
/** @var Bar $bar */
$bar = new EscapingProxy(new Bar());
echo $bar->qux;
echo $bar->getFoo()->baz;
echo $bar->getFoo()->getBaz();
class Foo {
public $baz = "baz";
public function getBaz() {
return $this->baz;
}
}
class Bar {
private $foo;
public $qux = "qux";
public function __construct() {
$this->foo = new Foo();
}
public function getFoo() {
return $this->foo;
}
}
class EscapingProxy {
private $object = null;
public function __construct($object) {
$this->object = $object;
}
public function __call($name, $args) {
return new self(call_user_func_array(array($this->object, $name), $args));
}
public function __get($name) {
return new self($this->object->$name);
}
public function __raw() {
return $this->object;
}
public function __toString() {
return $this->__escape();
}
public function __escape() {
// Just for demo purposes
return '-' . $this->object . '-';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment