Skip to content

Instantly share code, notes, and snippets.



Created Jun 12, 2012
What would you like to do?
Curry a function in PHP 5.3
class CurriedFunction {
public function __construct($callback/*[, $arg1][, $arg2][, $argN]*/) {
$this->args = func_get_args();
$this->callback = array_shift($this->args);
public function __invoke(/*[, $arg1][, $arg2][, $argN]*/) {
$moreArgs = func_get_args();
$args = array_merge($this->args, $moreArgs);
return call_user_func_array($this->callback, $args);
$world = new CurriedFunction('substr', 'Hello World.', 6);
echo $world(); // World.
echo '<br />';
echo $world(1); // W

This comment has been minimized.

Copy link

@jdalton jdalton commented Jun 12, 2012

Technically it's not currying but partial application:


This comment has been minimized.

Copy link
Owner Author

@kensnyder kensnyder commented Jun 12, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment