Skip to content

Instantly share code, notes, and snippets.

@jimbocoder
Created June 24, 2015 02:36

Revisions

  1. jimbocoder created this gist Jun 24, 2015.
    27 changes: 27 additions & 0 deletions php-compose.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    function compose() {
    $fxns = func_get_args();
    $outer = function($identity) { return $identity; };
    while($f = array_pop($fxns)) {
    if ( !is_callable($f) ) {
    throw new \Exception('This should be a better exception.');
    }
    $outer = function() use($f, $outer) {
    return $f(call_user_func_array($outer, func_get_args()));
    };
    }
    return $outer;
    }

    $password = 'PASSWORD';

    $rot13 = compose('str_rot13');
    // Hide our password from kid sister:
    print $rot13($password) . "\n";

    $doubleRot13 = compose($rot13, 'str_rot13');
    // Whoops, overshot that one:
    print $doubleRot13($password) . "\n";

    $uberHasher = compose('sha1', 'md5', 'crc32');
    // A REALLY SUPER ULTIMATE SECURE HASH:
    print $uberHasher($password) . "\n";