3 ms·
Why wrapped in a class? Seems entirely redundant or am I missing something?
by RedShift1 5y ago
Why wrapped in a class? Seems entirely redundant or am I missing something?
- leftnode 5y agoPerhaps you could extend the Pipe class and thus the pipe() method returns an object for chainability? That's the only reason I can immediately think of.
- adamrezich 5y agofree functions scary
- tored 5y agoPHP can't autoload functions, only classes. Thus if you wrap it in a static function inside a class you can autoload the the function by calling the class, Pipe::new(...), without doing a require "file.php".
- dten 5y agoYou can add a file import to composer, which is built into vendor/autoload.php automatically. I suppose this allows you to load this as a package...
- CiaranMcNulty 5y agoThat's not done lazily which is considered bad
- mkdirp 5y agoThat's what I was wondering too, especially considering there is a helper function that just wraps it. Seems rather unnecessary. <?php function pipe(...$args) { // Retrive first argument as initial value $value = array_shift($args); // Call the functions passing $value and assign its return foreach($args as $func) $value = $func($value); // Return computed value return $value; } echo pipe("10 WAYS to EAT more HEALTHY", fn($s) => strtolower($s), fn($s) => str_replace(" ", "-", $s) ); Works just as well.
- zowanet 5y agoPassing the first argument as part of the variadic array isn't necessary. Here's my version in only 66 bytes, what do I win? ;) <?php function pipe($v,...$p){foreach($p as$f)$v=$f($v);return$v;}