What does HackerNews think of functional-php?

Primitives for functional programming in PHP

Language: PHP

One nitpick from the article, PHP does have first class support for functions in the language. Functions are available in a few different ways:

- 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

Yep that's a great library, I'd like to see some of it in core. Same with this: https://github.com/lstrojny/functional-php which overlaps somewhat.

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.

Why not using namespaced functions instead of static methods all in the same class ? This would have allowed to add new functions. This would even have allowed to use the _ namespace.

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.