Skip to content

Instantly share code, notes, and snippets.

@andreiglingeanu
Forked from jdp/example.php
Created March 30, 2016 22:11
Show Gist options
  • Save andreiglingeanu/8f04495f6992a28bf6d7caa4567e1f7f to your computer and use it in GitHub Desktop.
Save andreiglingeanu/8f04495f6992a28bf6d7caa4567e1f7f to your computer and use it in GitHub Desktop.
Partial function application with PHP
<?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');
<?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()));
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment