Skip to content

Instantly share code, notes, and snippets.

@kensnyder
Created June 12, 2012 15:55
Show Gist options
  • Select an option

  • Save kensnyder/2918358 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown
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