0

Possible Duplicate:
What are namespaces?

from my understanding, name spaces allow you to have functions/variables with the same name inside different names spaces accross your scripts.

namespace productions\active;
class Slayer 
{
    function Username ()
    {
        $Username = "Test";
        return $Username;
    }
}
namespace productions\experimental;
class Slayer
{
    function Username()
    {
        $Username = "Experiemental"; 
        return $Username;
    }
}

But what functionality does this provide?

Furthermore, what would happen if I have public functions inside my classes which "live" inside a namespace?

Community
  • 1
  • 1
user1968541
  • 333
  • 1
  • 3
  • 12
  • 1
    Namespaces were supposed to be a more convenient alternative to eschewing *class name conflicts* manually with Under_Score_Prefixes. See also [http://en.wikipedia.org/wiki/Namespace_(computer_science)](http://en.wikipedia.org/wiki/Namespace_(computer_science)) – mario Jan 13 '13 at 15:05
  • 1
    Have you never encountered two identically named classes/functions? Say two `File` classes, one from a library and one from your own code? Namespaces solve that problem. – deceze Jan 13 '13 at 15:06
  • 2
    I personally see no practicality with using namespaces, if you needed to do two separate things, then you would create two separate functions, not go out the way and create namespaces to have the same function name. – user1968541 Jan 13 '13 at 15:08
  • 2
    Then you arguably seem to have little experience with real-world code. It's very typical to include **external libraries**. If classes and functions in those libraries are named somewhat conventionally, it's really not uncommon to have name clashes. `File` often is a sensible class name and it's not surprising to have two libraries use it. It's not all about *your* code. – deceze Jan 13 '13 at 15:11
  • 1
    A big project has more than 1 programmer. So each programmer has to write code independently, code that has to work with other programmers code. And it happens to have the same Class/Function name -> errors. – Mythul Jan 13 '13 at 15:14

1 Answers1

3

It would be obvious if you had to use two libraries with the same class names in the same project (yes, this may happen). With namespaces you can create alias for one of them, and use both without thouching library's core.

Marek M.
  • 3,799
  • 9
  • 43
  • 93
  • 1
    I personally see no practicality with using namespaces, if you needed to do two separate things, then you would create two separate functions, not go out the way and create namespaces to have the same function name. – user1968541 Jan 13 '13 at 15:07
  • It's common technique in almost all programming languages. As @deceze mentioned in comment to your question... Imagine - you're in a dire need to use some library which is huge. It defines class named File. Now - will you search and replace "File" string in all of this library files? – Marek M. Jan 13 '13 at 15:10
  • Probably not; but I would just change the name of the class which I have just created, also comment on my update in bold writing – user1968541 Jan 13 '13 at 15:11
  • 1
    And what if you used your class just everywhere in your project? Will you then find and replace? Or better example - both class names makes sense. Which name would you change? Namespacing gives you clean and elegant solution... – Marek M. Jan 13 '13 at 15:13
  • Can accept an answer within two minutes. – user1968541 Jan 13 '13 at 15:14