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 :(
Also, the double underscore prefix is reserved for magic methods:
PHP reserves all function names starting with __ as magical. It is
recommended that you do not use function names with __ in PHP unless you
want some documented magic functionality.
It's only explicitly reserved for functions, so you're technically ok here, but it still may cause confusion.
It was a bit of pick your poison between a gettext() alias collision, ugly namespace syntax, and potential confusion with magic methods. Hopefully too many people aren't confused.
But in a way, using the namespace like suggested is much more PHP-ish. After all, your class does not have sensible instances. There is not much OOP about it.
Where there is something not-much-OOP about something, PHP supplies global functions (compare md5() to 5 lines of Java code) instead of classes. So, using global functions would. in many ways, be much more PHP-ish than using static class methods (which is more Java-ish if you ask me).
Of course, you don't want to pollute the global namespace too much, so using the '_' namespace sounds excellent. I really like the idea.
Comments
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:
With static methods: 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.
Also, the double underscore prefix is reserved for magic methods:
It's only explicitly reserved for functions, so you're technically ok here, but it still may cause confusion.http://www.php.net/manual/en/language.oop5.magic.php
It was a bit of pick your poison between a gettext() alias collision, ugly namespace syntax, and potential confusion with magic methods. Hopefully too many people aren't confused.
You can always add functions via mixin. I'll see what I can do about the warnings.
But in a way, using the namespace like suggested is much more PHP-ish. After all, your class does not have sensible instances. There is not much OOP about it.
Where there is something not-much-OOP about something, PHP supplies global functions (compare md5() to 5 lines of Java code) instead of classes. So, using global functions would. in many ways, be much more PHP-ish than using static class methods (which is more Java-ish if you ask me).
Of course, you don't want to pollute the global namespace too much, so using the '_' namespace sounds excellent. I really like the idea.
Namespaces might be more PHP-ish, but I was primarily concerned with being Underscore-ish.
I'm also not entirely opposed to the namespace approach, but syntactically I would rather call:
thanHow can I add a mixin to the __ class without editing it ?
Just copying this from the docs, but you can pass an array of functions to mixin: