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?
1 Answer 1
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 c or even assembly, not interpreted languages like PHP.
-
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– BrianH2013年12月16日 18:18:16 +00:00Commented Dec 16, 2013 at 18:18
-
1Please see my edit above.elixenide– elixenide2013年12月16日 19:10:14 +00:00Commented Dec 16, 2013 at 19:10
-
2Did I mention that I'm making a PHP interface for the Large Hadron Collider? Otherwise, I believe this answer is sufficient, thank you.NobleUplift– NobleUplift2013年12月16日 20:22:48 +00:00Commented Dec 16, 2013 at 20:22
-
1Please don't create any quark stars or black holes! Glad to help.elixenide– elixenide2013年12月16日 20:34:06 +00:00Commented Dec 16, 2013 at 20:34
-
1Don't worry, only strangelets. We're creating hadron cannons- I mean, something sciency and not weaponish.NobleUplift– NobleUplift2013年12月17日 15:40:07 +00:00Commented Dec 17, 2013 at 15:40
new \Vendor\Namespace\Class()
without a require statement earlier in the method or class.