Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Vgl. http://de.wikipedia.org/wiki/Delegat_%28.NET%29#Beispiel_in_Visual_C.23
- namespace X
- {
- use Closure;
- abstract class Delegate
- {
- protected $closure;
- public function __construct(Closure $closure)
- {
- $this->closure = $closure;
- }
- public function __invoke()
- {
- return call_user_func_array($this->closure, func_get_args());
- }
- }
- }
- namespace Wikipedia\DelegateSample
- {
- use X\Delegate;
- class MyStringDelegate extends Delegate {}
- class MainClass
- {
- public static function main()
- {
- $mc = new MainClass();
- $delegateUpper = new MyStringDelegate(function ($s) use ($mc) {
- return $mc->upperFunction($s); });
- $delegateLower = new MyStringDelegate(function ($s) use ($mc) {
- return $mc->lowerFunction($s); });
- self::doSomething($delegateUpper, 'Hello World');
- self::doSomething($delegateLower, 'Hello World');
- }
- public static function doSomething(MyStringDelegate $delegate, $input)
- {
- echo $delegate($input), "\n";
- }
- public function upperFunction($s)
- {
- return mb_strtoupper($s);
- }
- public function lowerFunction($s)
- {
- return mb_strtolower($s);
- }
- }
- MainClass::main();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement