Skip to content

Instantly share code, notes, and snippets.

@azder
Forked from jdp/example.php
Last active August 29, 2015 14:26
Show Gist options
  • Save azder/6123b716daa387d19819 to your computer and use it in GitHub Desktop.
Save azder/6123b716daa387d19819 to your computer and use it in GitHub Desktop.
Partial function application with PHP
<?php
class PartialCallable {
function __construct($callable, $args) {
$this->callable = $callable;
$this->args = $args;
}
function __invoke() {
call_user_func_array($this->callable, array_merge($this->args, func_get_args()));
}
}
<?php
function partial_function() {
$applied_args = func_get_args();
return function() use($applied_args) {
return call_user_func_array('call_user_func', array_merge($applied_args, func_get_args()));
};
}
<?php
function foo($a, $b) {
echo "a: {$a} b: {$b}\n";
}
$foo_caller = new PartialCallable('foo', array('A'));
$foo_caller('B');
$foo_caller = partial_function('foo', 'A');
$foo_caller('B');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment