6

I have a few general use functions that do not really make sense in any class as static methods. I would like to encapsulate them under a namespace so there are no conflicts with functions defined in the global scope. For my namespaced classes, I follow the widely-adopted pattern where a class such as \My\Namespaced\MyClass exists in My/Namespaced/MyClass.php on the include path.

Is there a best practice for where namespaced functions should be placed? Right now I'm putting them in "functions.php" within the directory holding classes under the same namespace. For example \My\Namespaced\myFunction exists in My/Namespaced/functions.php.

Also, is there any way to autoload these functions in the same way that classes are autoloaded?

rr.
  • 6,484
  • 9
  • 40
  • 48

2 Answers2

5

Also, is there any way to autoload these functions in the same way that classes are autoloaded?

Not for global functions, but if ...

Is there a best practice for where namespaced functions should be placed?

I would consider using objects instead a "best practice", however we all know that it's not totally true.

There is no autoloading for global functions, you can encapsulate functions into classes as static functions and then the autoloader will kick into action. So that might be a suggestion, however you should be clear about the implications those static functions have for your overall design.

So to say: If you're okay with global functions, then you might be okay with global static class functions. They will break if you change a class's name (like with any global function name), you have however created something that can autoload and is compatible with your file naming scheme.

Edit: When I write global, I mean the fully qualified name of a function, that's the one starting with \. See Name resolution rules Docs.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • None of this is global, it's using PHP 5.3 namespaces. – igorw Aug 29 '11 at 19:01
  • @igorw: What do you mean? What is not global? I just don't get it, feel free to elaborate. – hakre Aug 29 '11 at 19:26
  • maybe I'm not getting what you mean by `global`. All I'm saying is that the functions in question are not in the global namespace. – igorw Aug 29 '11 at 19:31
  • Ah okay. I mean fully qualified function names, those are always global. They start with `\\`. Anyway global functions are those that are globally accessible, they have a unique name in the global namespace, even if you define them not fully qualified. Edited the answer to make that more visible. – hakre Aug 29 '11 at 19:41
  • I'm not a huge fan of attaching a function to a class that does not carry out behavior relevant to the class. It also adds cruft, which I'm also not a fan of. I think with what PHP provides, the solution will come down to a matter of style, as you allude to in your post. – rr. Aug 29 '11 at 20:47
  • @rr: Well, that's merely what I wanted to say: I'm not a fan either of static class methods, if you want auto-loading for global functions that's the only way though. The level of cruft however is considerable low if I think about it. Some lines of code only (two?). Additionally you can actually make use of visibility as well, so maybe that will help to create (is it advisable?) behaviour. – hakre Aug 29 '11 at 20:55
  • @rr: Your question is quite broad, maybe you want to leave it unanswered to gather some more attraction. – hakre Aug 29 '11 at 21:42
2

Unfortunately there is no autoloading for functions (because the PHP devs decided so...), thus you must think about, how you get the function files include. For example you can use a functional for that (like importFunction($namespace); It would just map the namespace against the filename and include the file), or you can include every file, that contains functions at once (at startup or such).

KingCrunch
  • 128,817
  • 21
  • 151
  • 173