PHP gets Namespaces

Oct
3

Namespaces can be very useful, and if you're familiar with ASP.net then you will no doubt have used them at some point or another. One thing this makes possible is having multiple classes with the same name but in a different namespace. It also has other more obvious uses like being able to group classes together.

The syntax is slightly different though (as you'd expect). The following is an example in C#.net:

namespace Neo {
  namespace Utils {
    public class Colour {
    }
  }
}

And in PHP this would be:

namespace Neo::Utils;
class Colour {
}

As with ASP.net it must be instantiated before it can be used.

Example:

$my_var = new Neo::Utils::Colour()

Also you can only define one namespace within a file.

your comments - Post a comment

blog comments powered by Disqus