What does HackerNews think of functional-php?
Primitives for functional programming in PHP
- Normal function declaration in global and "module"/namespace scope.
- Anonymous functions that can also be assigned to variables (except in a few cases, like object properties)
- Anything extending the Closure class
The last one is obviously a nod at what is going on under the hood, but as far as the developer writing the code is concerned, it doesn't matter.
A library even exists to explicitly provide some functional programming functions under a namespace for easy inclusion with other projects. There is no requirement that all functions must be defined in a namespace, as evidenced by the first 10 years of crappy PHP code littered with functions in the global namespace!
https://github.com/lstrojny/functional-php
Edit: formatting
The no-brainers to me are the missing array_some()/array_any() array_every()/array_all() and an array_search() that takes a predicate and iterable. I do these now in userland but an optimized native version would be nice.
With namespaces:
namespace _;
function each() {
}
// chaining can be achieved with this:
class Wrapper {
private $coll;
function __construct($coll) {
$this->coll = $coll;
}
function __call($name, $args) {
array_unshift($this->coll,$args);
return new self(call_user_func_array('_\\'.$name, $args));
}
}
function __($coll) {
return new Wrapper($coll);
}
use _;
_\each(...);
// or
__($coll)->each(...);
// I can add new functions
namespace _;
function something(){}
With static methods: class __ {
function each() {
}
}
// throws strict errors
__::each(...);
// can't add functions
Also it seems that the normal way to use the library is to call isntance methods statically, which triggers many warnings with E_STRICT :(https://github.com/lstrojny/functional-php also brings some functional stuff to php.