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 recently started using namespaces in PHP. When I first saw them, I thought that their syntax was ugly and I'd never use them. However, I created an autoloader (spl_autoload_register) that makes it so that I never have to write an include/require statement again.

I like namespaces, but is there any objective benefit over include/require statements, or are they the exact same method to accomplish the same goal?

share|improve this question
    
Namespaces prevent collisions with third-party libraries. –  Mathew Foscarini Dec 16 '13 at 17:09
    
Right now the primary benefit for me concerning namespaces is the autoloading. For instance, I use in-line new \Vendor\Namespace\Class() without a require statement earlier in the method or class. –  NobleUplift Dec 16 '13 at 17:22
2  
Many PHP libraries use autoloading without namespaces. PHP should make namespaces mandatory. –  Mathew Foscarini Dec 16 '13 at 17:25
    
No argument from me. I'd like to see all of these changes listed here. –  NobleUplift Dec 16 '13 at 19:02

1 Answer 1

up vote 8 down vote accepted

Namespaces are not just for autoloading classes. They also prevent naming conflicts. In fact, that's their primary purpose.

Say you have a project that needs a class named User, to store info about users of your application, but a plugin also uses a (different) class named User to store information. Namespaces let you create your class within one namespace (say, MyApp) and let the plugin use another namespace (say, CoolPlugin). Code within the MyApp space can just refer to User (e.g., new User();), and so can code in the CoolPlugin space; each will get the expected result. When you need to use code from another namespace, you just prefix it. For example, code in the CoolPlugin space can access the User class in MyApp via new \User\MyApp();

The alternative is that every class needs a complex name everywhere, such as class MyApp_User and class CoolPlugin_User. Namespaces let you simplify things most of the time and avoid naming conflicts all of the time.

Edit: To answer the question, "Is there any performance difference between the two?"

Not a meaningful one, no. I haven't benchmarked it, but there is probably a difference on the nanosecond level. That said, sacrificing code quality for super-small performance tweaks is not a good strategy, so you should use namespaces regardless. For benchmarks of similar kinds of problems, see PHPbench.com and this StackOverflow answer.

Your code needs to be incredibly tight and incredibly time-sensitive (think high-frequency trading or managing nuclear reactions) before you need to worry about micro-optimizing it in this kind of context. If it really is that time-sensitive, you should probably be coding in or even , not interpreted languages like PHP.

share|improve this answer
    
Good point! I'm actually already using this functionality and I didn't think twice about it. –  NobleUplift Dec 16 '13 at 17:18
    
+1 I simply note that every project begins seeming like it couldn't possibly need namespaces, and then easily grows to the point that it becomes ridiculous to hack around things to work without them. Not having namespaces is basically the same as declaring every variable global, as applied to classes. On bigger code bases or ones that make liberal use of libraries, it becomes a total mess. –  BrianDHall Dec 16 '13 at 18:18
    
Is there any performance difference between the two? –  NobleUplift Dec 16 '13 at 19:03
    
Please see my edit above. –  Ed Cottrell Dec 16 '13 at 19:10
    
Did I mention that I'm making a PHP interface for the Large Hadron Collider? Otherwise, I believe this answer is sufficient, thank you. –  NobleUplift Dec 16 '13 at 20:22

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.