Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm at the pre-release stage of an open-source PHP project, one which I hope will be used by other developers in their own projects. The project doesn't currently support namespaces and I'm trying to evaluate if it should use namespaces or the PEAR naming convention of Dir_Subdir_Class, which seems to have all of the same technical benefits without some of the drawbacks. To be honest, it's not an easy choice.

Some points of consideration against namespaces:

  • One of the ways that my project is trying to differentiate itself by providing a simpler API than other similar projects. Because namespaces are new and also because they are more complicated than the PEAR naming convention, introducing them into the codebase will make my project less simple to use. By implementing them, I lose some differentiation in terms of ease-of-use.
  • While I can see some benefits to namespaces, they don't seem to solve a problem that needs to be solved in a modern PHP product that uses the PEAR naming convention. Naming conflicts while using my project should be minimal if not non-existent.
  • This article gives me some pause in adopting namespaces as their implementation has been less than stellar.
  • I'm also hesitant to jump on a bandwagon that may not go anywhere. As namespaces are a new feature to PHP, I'm not convinced yet that they will become standard.
  • Compatibility. Almost all of the PHP code that has ever been written does not use namespaces as it's a new feature. Other libraries would be incompatible without a conversion.

Some points for using namespaces:

  • Perception. If namespaces become standard and a best-practice, my project could quickly become viewed as unprofessional and obsolete without them.
  • Competition. While some competing PHP projects are beginning to use namespaces in their latest versions, many have yet to make the leap. Doing so now could give my project a leg-up on other projects.
  • Future work would be easier if I made the switch now before the project goes public rather than after, where I would have to support two versions of it for a while.
  • I want to support best-practices and if namespaces become a best practice for PHP, my project should make use of them.

From what I can tell, you must choose one way or the other; you can't do both. Are there any points that I haven't considered? Are there any objective signs (no flamewars, please) that point toward or against namespaces becoming the professional standard for PHP? I would appreciate any insight or resources that you would be willing to share as I need to make a decision soon.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Two signs that namespaces in PHP are here to stay:

  1. The PEAR naming scheme was abandoned in favor of namespaces in PEAR2.
  2. One of the stated goals of Zend Framework 2.0 is to be an exemplar of PHP 5.3 usage, by fully utilizing namespaces, amongst other things. I consider this as a strong indication that Zend is fully committed to namespaces and will continue to support and evolve them (hopefully to the better).

I fully agree with you that the current implementation of namespaces is lacking, to say the least, but your arguments against using them are not that solid. Even in their current form, namespaces provide for:

  • Better code organization,
  • Avoiding naming collisions,
  • Context for classes, functions and constants.

Keep in mind that most arguments against PHP namespaces are by comparison to implementations in other languages and not against their actual merits as a feature.

share|improve this answer
    
+1 for the links. Could you expand on the better code organization and context points? I have a little bit of a hard time understanding how it provides better code organization. It seems that most projects will stick to the 1:1 class to file structure grouped in logical directories, the same as you would use in the PEAR naming scheme. Even ZF2 looks like it is using the same directory and file structures in most cases, but just now with namespaces. It's different, but I don't necessarily see how it's better or more organized than well-named directories and files in the first place. –  VirtuosiMedia Nov 9 '11 at 8:29
    
I have trouble with the context point as well. If anything, it seems like namespaces remove context to have a more succinct coding style rather than adding context. As an example, if I instantiate a new class deep in a file, I now need to find where the namespace was declared to figure out which class it is rather than having it immediately apparent from the class name. Unless I'm missing something, it seems that namespaces are less verbose, but at the expense of clarity. –  VirtuosiMedia Nov 9 '11 at 8:37
    
@VirtuosiMedia The PEAR and the old ZF naming schemes emulate namespaces, so my three points are somewhat true for them too. What I'm trying to point out is that if those points are true in the current implementation of PHP namespaces, then their small snafus aren't enough to not use them. With better code organization I mean mostly across files, adopting a sensible and logical structure and hierarchy for your classes, something that's of course feasible without namespaces. But namespaces (and schemes that emulate them) are a feature that help you achieve all three points at once. –  Yannis Rizos Nov 9 '11 at 8:37
    
Gotcha. Thanks for the clarification. Do you see significant advantages for actual namespaces over emulated namespaces? –  VirtuosiMedia Nov 9 '11 at 8:42
1  
@VirtuosiMedia All the points for using namespaces you make on the question are valid. To that I would add that PHP namespaces help your code feel a little bit more natural to coders from different backgrounds, something that might be invaluable in a large project. Also there isn't a standard for emulated namespaces, sure most of them are about the same but using actual namespaces ensures that everyone is using the same schema. BTW there is a namespacer out there you could use to namespacify your code. –  Yannis Rizos Nov 9 '11 at 8:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.