4

Now that I am using namespaces in my php files which match the file's path, I have to append a namespace to pretty much every class instantiation. The namespaces definitely help my autoloader find files, but the code is becomming harder to read/write. aren't namespaces supposed to simplify my code? why is it making things more complicated? Am I using it wrong? Not sure I see how using a namespace is much better than having super long class names...

example:

file: /root_path/init.php

<?php
    namespace root_path;
    $foo = new \sub_path\bar();
?>

file: /root_path/sub_path/bar.php

<?php
    namespace root_path\sub_path;
    class bar {

    }
?>
  • Never had a particular need for them myself. You dont HAVE to use them if you dont think they are helping. – GrandmasterB Apr 14 '11 at 19:22
  • @GrandmasterB I am using namespaces primarily for the class autoloader. Many classes are found in subfolders, and namespaces seemed the easiest way for the autoloader to find them. – dqhendricks Apr 14 '11 at 19:29
6

It looks like you're using it right, but you might not have a need for it.

Namespacing allows you to use function and symbol names without worrying about whether a third-party library declares the same thing (which would cause a fatal error in PHP). You can take comfort in knowing that, because you've namespaced everything, there's no chance for collision.

But if you're in a position where you're going to know and account for all possible symbols, you're not going to get a whole lot of value from namespaces. Edit: And as G3D mentions in the comments, your specific use case is covered by PEAR's naming conventions.

However, relying on prefixes might not work as you'd expect if you start introducing a lot of unknowns.

This comes up in Drupal: let's say you've created a module that's named views. Well, Drupal provides a hook system, so you can do things like implement the function hook_foo() by replacing hook with your module's name.

So now you have an implementation of a hook named views_foo(). That's all well and fine as long as you didn't need an internal function named views_foo().

But let's say there's a new module that extends your module and names itself views_awesome. If views_awesome wants to implement hook_foo(), it'd create a new function named views_awesome_foo().

Ah, but I didn't tell you about another hook already defined:hook_awesome_foo(). So, without namespacing, you have a collision: views_awesome_foo() can refer to view_awesome's implementation of hook_foo or views's implementation of hook_awesome_foo().

In this scenario, namespacing is really helpful. Instead of prefixing hook implementations, you can do:

\views\hook_foo()
\views_awesome\hook_foo()

and avoid any ambiguity.

  • I am using namespaces primarily for the class autoloader. Many classes are found in subfolders, and namespaces seemed the easiest way for the autoloader to find them. – dqhendricks Apr 14 '11 at 19:29
  • 1
    You don't need namespaces for that, the Zend/Pear naming convention (what you using) allows that. – DBlackborough Apr 14 '11 at 19:44
  • @G3D indeed, added that note to the answer. – user8 Apr 14 '11 at 19:58
0

They do, in a way, but they are critical for long term code reuse. Otherwise you have to make sure that there are no variable namespace collisions, and that becomes extremely cumbersome.

Lack of namespaces has been a very big issue with php, especially given its very loose scoping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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