fork(1) download
  1. <?php
  2.  
  3. class FunctionHolder
  4. {
  5.  
  6. private $functions;
  7.  
  8. public function __construct()
  9. {
  10. $this->functions = [];
  11. }
  12.  
  13. /**
  14.   * @param callable $function
  15.   * @param string $functionName
  16.   * @throws Exception
  17.   */
  18. public function registerFunction($function, $functionName)
  19. {
  20. if (!is_callable($function)) {
  21. throw new Exception ('Not a function');
  22. }
  23. $this->functions[$functionName] = $function;
  24. }
  25.  
  26. /**
  27.   * @param string $functionName
  28.   * @param array $parameters
  29.   */
  30. public function run($functionName, $parameters)
  31. {
  32. call_user_func_array($this->functions[$functionName], $parameters);
  33.  
  34. }
  35. }
  36.  
  37. $functionHolder = new FunctionHolder();
  38. /**
  39.  * @param int $number
  40.  * @param string $string
  41.  */
  42. $function = function ($number, $string) {
  43. echo "Номер $number";
  44. echo "\nСтрока $string";
  45. };
  46. $functionHolder->registerFunction($function, 'printNumbers');
  47. $functionHolder->run('printNumbers', [12, 'попугай']);
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Номер 12
Строка попугай