@ 2018-03-03T20:10:20Z <?php
class Inflector
{
private $methods = [];
public function __construct(array $methods)
{
$this->methods = $methods;
//var_dump($this->methods);
}
public function __call(string $methodName, $test)
{
if (isset($this->methods[$methodName])) {
return $this->methods[$methodName]($test);
}
throw new InflectionDeceptionException();
}
}
class InflectionDeceptionException extends \Exception
{
public function __construct($message = "Say what?")
{
return parent::__construct($message);
}
}
$a = new Inflector([
'foo' => function($val){echo sprintf('%s calls foo()', $val[0] ?? 'none!').PHP_EOL;},
'bar' => function($val){echo sprintf('%s calls bar()', $val[0] ?? 'none!').PHP_EOL;},
'baz' => function($val){echo sprintf('%s calls baz()', $val[0] ?? 'none!').PHP_EOL;},
]);
$a->foo('Ahnold');
$a->bar('Elvis');
$a->baz('Lerhman');
$a->theBreaks('Who');
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.1.0 - 7.1.20 , 7.2.0 - 7.2.33 , 7.3.16 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.27 , 8.2.0 - 8.2.17 , 8.3.0 - 8.3.4 Ahnold calls foo()
Elvis calls bar()
Lerhman calls baz()
Fatal error: Uncaught InflectionDeceptionException: Say what? in /in/kLdOp:20
Stack trace:
#0 /in/kLdOp(43): Inflector->__call('theBreaks', Array)
#1 {main}
thrown in /in/kLdOp on line 20
Process exited with code 255 . preferences:dark mode live preview
149.46 ms | 403 KiB | 154 Q