Skip to content

Comment on Underscore.phpparent

Comments

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:

  __::foo('bar');
than
  use _;  
  __\foo('bar');

How 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:

  __::mixin(array(
    'capitalize'=> function($string) { return ucwords($string); }
  ));
  __::capitalize('moe'); // 'Moe'
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.