3
use level1\level2\level3;

Can someone explain with a simple demo ?

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

7 Comments

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 {}
So the only functionality of use is a shorthand?
Its only a shorter notation for namespace
It's interesting that my\stuff\nested will work even if my\stuff doesn't exist.Is this a bug?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.