tags:

views:

75

answers:

2
use level1\level2\level3;

Can someone explain with a simple demo ?

+5  A: 

To clear up any confusion regarding different syntax use, namespaces support only two syntaxes, either bracketed or simple-combination both will work. I suggest if you use one over the other, be consistent.

<?php
namespace my\stuff\nested {  // <- bracketed syntax
 class foo {}
}
?>

It creates a class foo inside of the nested namespace with bracketed syntax ({}), it is equivalent to

<?php
namespace my\stuff {  // bracketed syntax but with a nested look
  namespace nested {
     class foo {}
  }
}
?>

You can also use nested namespaces with simple-combination syntax (;)

<?php
namespace mine;
use ultra\long\ns\name;  // <- simple-combination syntax

$a = name\CONSTANT;
name\func();
?>

PHP: FAQ: things you need to know about namespaces

Anthony Forloney
Seems `{}` after namespace is not necessary?
Nope, it is not necessary. It show how you can create a class inside of a namespace, hence the `{}`
Anthony Forloney
So the only functionality of `use` is a shorthand?
Its only a shorter notation for `namespace`
Anthony Forloney
It's interesting that `my\stuff\nested` will work even if `my\stuff` doesn't exist.Is this a bug?
Nope its not a bug, you actually *are* declaring the namespace `my\stuff`
Anthony Forloney
Sorry,can you elaborate?
When you type in your code `hey\whats\up` as a namespace, you are declaring the namespace to be `hey\whats\up`, regardless of whether `hey` or `whats` exists or does not exist. It is more of a hierarchy approach. So inside of that namespace if I were to initialize a constant called `Dude`, I would access that constant by `hey\whats\up\Dude`.
Anthony Forloney