Skip to content

Instantly share code, notes, and snippets.

@kensnyder
Created June 12, 2012 15:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kensnyder/2918358 to your computer and use it in GitHub Desktop.
Save kensnyder/2918358 to your computer and use it in GitHub Desktop.
Curry a function in PHP 5.3
<?php
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
@kensnyder
Copy link
Author

kensnyder commented Jun 12, 2012 via email

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