Skip to content

Instantly share code, notes, and snippets.

@slava-konashkov
Last active July 30, 2020 08:26
Show Gist options
  • Select an option

  • Save slava-konashkov/11fceb0138ece07016c3fdb5ca6392c2 to your computer and use it in GitHub Desktop.

Select an option

Save slava-konashkov/11fceb0138ece07016c3fdb5ca6392c2 to your computer and use it in GitHub Desktop.
Invoker.php
<?php
class Invoker {
private $args = [];
function __invoke($a) {
if (is_callable($a)) {
$result = array_shift($this->args);
foreach ($this->args as $arg) {
$result = $a($result, $arg);
}
return $result;
}
$this->args[] = $a;
return $this;
}
}
function calc($a) {
return (new Invoker())($a);
}
$sum = function($a, $b) { return $a + $b; };
var_dump(calc(5)(3)(2)($sum)); // 10
var_dump( calc(1)(2)($sum)); // 3
var_dump( calc(2)(3)('pow')); // 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment