Skip to content
Permalink
Browse files

lib/moodlelib MDL-21130 added helper partial() function

this helps to create callbacks with partially bound arguments. woot!
  • Loading branch information...
Penny Leach
Penny Leach committed Dec 17, 2009
1 parent 9468534 commit 0a2a36f7374d7e1c81f8905307a049b0b62992bc
Showing with 42 additions and 0 deletions.
  1. +42 −0 lib/moodlelib.php
@@ -9217,3 +9217,45 @@ function check_consecutive_identical_characters($password, $maxchars) {
return true;
}
/**
* helper function to do partial function binding
* so we can use it for preg_replace_callback, for example
* this works with php functions, user functions, static methods and class methods
* it returns you a callback that you can pass on like so:
*
* $callback = partial('somefunction', $arg1, $arg2);
* or
* $callback = partial(array('someclass', 'somestaticmethod'), $arg1, $arg2);
* or even
* $obj = new someclass();
* $callback = partial(array($obj, 'somemethod'), $arg1, $arg2);
*
* and then the arguments that are passed through at calltime are appended to the argument list.
*
* @param mixed $function a php callback
* $param mixed $arg1.. $argv arguments to partially bind with
*
* @return callback
*/
function partial() {
if (!class_exists('partial')) {
class partial{
var $values = array();
var $func;
function __construct($func, $args) {
$this->values = $args;
$this->func = $func;
}
function method() {
$args = func_get_args();
return call_user_func_array($this->func, array_merge($this->values, $args));
}
}
}
$args = func_get_args();
$func = array_shift($args);
$p = new partial($func, $args);
return array($p, 'method');
}

0 comments on commit 0a2a36f

Please sign in to comment.
You can’t perform that action at this time.