Let me give you two specific cases where I think importing a function
significantly improves the readability of code.

A) Tiny libraries

I recently released a "library" that really just consists of two functions.
Those functions are named compose() and pipeline(), and the library is named
"igorw/compose". I didn't want to risk conflicts in the global namespace, so I
just put them in the "igorw" namespace.

Now users need to do this:

    $x = igorw\compose('foo', 'bar', 'baz');

It's distracting to have my vendor name in there every time they call the
function. They can alias it, but then they just need to come up with a random
word that makes no sense.

    compose\compose(...);
    c\compose(...);
    foo\compose(...);

B) Domain specific languages

I hope I'm not mis-using the term "DSL" here, so just let me explain what I
mean. When building some kind of representation of something, it can sometimes
be quite nice to have a set of functions representing the elements, and nest
those calls.

An example of this could be building HTML (you can apply it to building
workflows, business rules, graphs, configuration, etc).

    use html\div, html\p, html\em;

    $html = div(p('Some', em('Text')));

Having to prefix the namespace in either of these cases leads to a lot of
noise. Hopefully that gives you a better idea of where I'm coming from.

Regards,

Igor


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to