]> git.mjollnir.org Git - moodle.git/commitdiff
lib/moodlelib MDL-21130 added helper partial() function
authorPenny Leach <penny@mjollnir.org>
Thu, 17 Dec 2009 08:25:45 +0000 (09:25 +0100)
committerPenny Leach <penny@mjollnir.org>
Thu, 17 Dec 2009 08:25:45 +0000 (09:25 +0100)
this helps to create callbacks with partially bound arguments. woot!

lib/moodlelib.php

index e11b6f51b2eb96bd2ee7713482fb136a4ef78789..83fbd36392837ed9f4f539a9431e5d5034ad57f5 100644 (file)
@@ -9218,3 +9218,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');
+}