Skip to content

Instantly share code, notes, and snippets.

@kensnyder

kensnyder/curry.php

Created Jun 12, 2012
Embed
What would you like to do?
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
@jdalton

This comment has been minimized.

Copy link

@jdalton jdalton commented Jun 12, 2012

Technically it's not currying but partial application:
http://en.wikipedia.org/wiki/Currying#Contrast_with_partial_function_application

@kensnyder

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