Last active
October 25, 2019 09:28
-
-
Save gollilla/c22115bfd1f5f58470ffc02c9c30a6d2 to your computer and use it in GitHub Desktop.
This file contains 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 Switcher { | |
public static $calls = []; | |
public static function register($case, Closure $callback){ | |
self::$calls[$case] = new Executor($callback); | |
} | |
public static function onCase($case){ | |
return isset(self::$calls[$case]) ? self::$calls[$case] : new Executor(function(){}); | |
} | |
} | |
class Executor { | |
public function __construct(Closure $callback){ | |
$this->callback = $callback; | |
} | |
public function call(...$args){ | |
$callback = $this->getCallBack(); | |
return call_user_func_array($callback, $args); | |
} | |
public function getCallBack(){ | |
return $this->callback; | |
} | |
} | |
Switcher::register(1, function($a,$b){ | |
echo "$a:$b wwwww\n"; | |
}); | |
Switcher::register(2, function(){ | |
return 1 + 1; | |
}); | |
Switcher::onCase(1)->call(2,4); | |
echo Switcher::onCase(2)->call(2,3); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment