Created
December 5, 2013 19:16
-
-
Save loganlinn/7811493 to your computer and use it in GitHub Desktop.
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 | |
class TestClass { | |
private $id; | |
public function __construct($id) { | |
$this->id = $id; | |
} | |
public function getPublic() { | |
return "public {$this->id}"; | |
} | |
protected function getProtected() { | |
return "protected {$this->id}"; | |
} | |
private function getPrivate() { | |
return "private {$this->id}"; | |
} | |
public function getClosure() { | |
return function() { | |
echo | |
$this->getPublic(), "\n", | |
$this->getProtected(), "\n", | |
$this->getPrivate(), "\n"; | |
}; | |
} | |
} | |
$inst1 = new TestClass(1); | |
$inst2 = new TestClass(2); | |
$c1 = $inst1->getClosure(); | |
$c1(); | |
$c2 = $c1->bindTo($inst2); | |
$c2(); | |
// public 1 | |
// protected 1 | |
// private 1 | |
// public 2 | |
// protected 2 | |
// private 2 | |
class TestClassDecorator extends TestClass { | |
public function getPublic() { | |
return "\t<value>" . parent::getPublic() . "</value>"; | |
} | |
public function getProtected() { | |
return "\t<value>" . parent::getProtected() . "</value>"; | |
} | |
public function getPrivate() { | |
return "\t<value>" . parent::getPrivate() . "</value>"; | |
} | |
public function getClosure() { | |
return function() { | |
$c = parent::getClosure(); | |
echo "<decorated>", "\n"; | |
$c(); | |
echo "</decorated>", "\n"; | |
}; | |
} | |
} | |
$inst1 = new TestClassDecorator(1); | |
$inst2 = new TestClass(2); | |
$c1 = $inst1->getClosure(); | |
$c1(); | |
$c2 = $c1->bindTo($inst2); | |
$c2(); | |
// <decorated> | |
// <value>public 1</value> | |
// <value>protected 1</value> | |
// private 1 | |
// </decorated> | |
// <decorated> | |
// public 2 | |
// protected 2 | |
// private 2 | |
// </decorated> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment