Re: Importing namespaced functions

From: Date: Sun, 21 Apr 2013 22:36:15 +0000
Subject: Re: Importing namespaced functions
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-67104@lists.php.net to get a copy of this message
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

« previous php.internals (#67104) next »