Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

We do this thing in PHP with classes. See below. What is the advantage - if any - of namespaces?

  class yo 
  {
   function isBig($number)
   {
      return ($number>1000);
   }
  }

  if (yo::isBig(50)) echo '50 is big';


Classes are a physical grouping of logical unit. Namespace is a logical grouping of classes.


isBig(50) is more readable than yo::isBig(50) in my opinion.


because colons are ugly. yo.isBig(50) is much more readable.

Now, assuming somebody else decides to have a function called isBig(), which does something very different.

The only way to use both in php, would be if they had different names (hence the countless of functions that do very similiar things, but have different names).

While in languages that have a propper namespace you can do: numModule.isBig(50) floatModule.isBig(50) --etc

Also, in java you can import a class directly.

You can use com.mycompany.numClass.isBig(50)

or import com.mycompany.numClass;

and use numClass.isBig(50)

if there are two classes with the same name, from totally different packages (namespaces), then you will have to use one of them the long way ( com.mycompany.numClass.isBig(50) ) in order to avoid namespaces conflicts.

Java does a decent job in all this. Python is even less strict (you can import a module with (import as)a nd name it something different, when you use it), which allows for great flexibility, but also more confusion if you don't know what you are doing. But if people adere to the python mantra to keep things flat, than it should not be a problem.

So, overrall namespaces are very useful, especially in any large app. But, you haven't seen them on php yet, as the php crowd could care less for them. Plus Php was ment just for simple page processing and apps, and nothing too complicated.


OK, now put your classes in a namespace




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: