Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Created July 31, 2013 13:28
Show Gist options
  • Save ackintosh/6121960 to your computer and use it in GitHub Desktop.
Save ackintosh/6121960 to your computer and use it in GitHub Desktop.
Compose functions in PHP.
<?php
class Compose
{
public static function _()
{
$callables = func_get_args();
return array_reduce($callables, function ($a, $b) {
if ($a === null) {
return function () use ($b) {
return call_user_func_array($b, func_get_args());
};
} else {
return function ($arg) use ($a, $b) {
return call_user_func($b, $a($arg));
};
}
});
}
}
$mapUcfirst = function ($arg) {
return array_map('ucfirst', $arg);
};
$splitWithUnderscore = function ($arg) {
return explode('_', $arg);
};
$join = function ($arg) {
return implode('', $arg);
};
$camelize = Compose::_($splitWithUnderscore, $mapUcfirst, $join);
var_dump($camelize('man_with_a_mission'));
// ManWithAMission
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment