<?php
class FunctionHolder
{
private $functions;
public function __construct()
{
$this->functions = [];
}
/**
* @param callable $function
* @param string $functionName
* @throws Exception
*/
public function registerFunction($function, $functionName)
{
throw new Exception ('Not a function');
}
$this->functions[$functionName] = $function;
}
/**
* @param string $functionName
* @param array $parameters
*/
public function run($functionName, $parameters)
{
}
public function __call($functionName, $parameters)
{
if (isset($this->functions[$functionName])) { } else {
throw new Exception ("method $functionName not found in " . __CLASS__);
}
}
}
$functionHolder = new FunctionHolder();
/**
* @param int $number
* @param string $string
*/
$function = function ($number, $string) {
echo "Номер $number";
echo "\nСтрока $string";
};
$functionHolder->registerFunction($function, 'printNumbers');
//$functionHolder->run('printNumbers', [12, 'попугай']);
$functionHolder->printNumbers(12, 'попугай');
try {
$functionHolder->printNumbersTwo(12, 'попугай');
} catch (Exception $e) {
echo PHP_EOL . $e->getMessage() . PHP_EOL;
}
PD9waHAKCmNsYXNzIEZ1bmN0aW9uSG9sZGVyCnsKCiAgICBwcml2YXRlICRmdW5jdGlvbnM7CgogICAgcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0KCkKICAgIHsKICAgICAgICAkdGhpcy0+ZnVuY3Rpb25zID0gW107CiAgICB9CgogICAgLyoqCiAgICAgKiBAcGFyYW0gY2FsbGFibGUgJGZ1bmN0aW9uCiAgICAgKiBAcGFyYW0gc3RyaW5nICRmdW5jdGlvbk5hbWUKICAgICAqIEB0aHJvd3MgRXhjZXB0aW9uCiAgICAgKi8KICAgIHB1YmxpYyBmdW5jdGlvbiByZWdpc3RlckZ1bmN0aW9uKCRmdW5jdGlvbiwgJGZ1bmN0aW9uTmFtZSkKICAgIHsKICAgICAgICBpZiAoIWlzX2NhbGxhYmxlKCRmdW5jdGlvbikpIHsKICAgICAgICAgICAgdGhyb3cgbmV3IEV4Y2VwdGlvbiAoJ05vdCBhIGZ1bmN0aW9uJyk7CiAgICAgICAgfQogICAgICAgICR0aGlzLT5mdW5jdGlvbnNbJGZ1bmN0aW9uTmFtZV0gPS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