21

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?

NobleUplift
  • 815
  • 3
  • 7
  • 16
  • 1
    Namespaces prevent collisions with third-party libraries. – Reactgular Dec 16 '13 at 17:09
  • 1
    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
  • 3
    Many PHP libraries use autoloading without namespaces. PHP should make namespaces mandatory. – Reactgular Dec 16 '13 at 17:25
  • No argument from me. I'd like to see all of these changes [listed here](http://philsturgeon.co.uk/blog/2013/01/php-6-pissing-in-the-wind). – NobleUplift Dec 16 '13 at 19:02

1 Answers1

24

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 \MyApp\User();

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.

elixenide
  • 442
  • 1
  • 6
  • 17
  • 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
    +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. – BrianH Dec 16 '13 at 18:18
  • Is there any performance difference between the two? – NobleUplift Dec 16 '13 at 19:03
  • 1
    Please see my edit above. – elixenide Dec 16 '13 at 19:10
  • 2
    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
  • 1
    Please don't create any quark stars or black holes! Glad to help. – elixenide Dec 16 '13 at 20:34
  • 1
    Don't worry, only strangelets. We're creating hadron cannons- I mean, something sciency and not weaponish. – NobleUplift Dec 17 '13 at 15:40