3

Suppose I have a top-level namespace \Outer and I have another sub-namespace \Outer\Inner and I have another top-level namespace \Inner

and in a class in \Outer I use Inner like this

use Inner;

then which Inner will be used?

\Outer\Inner // ( sub-namespace )

or the

\Inner  // ( top-level namespace )

I am confused because php said that the \ was optional for top-level namespaces?

2
  • use Inner and use \Inner both are different. First one is sub-namespace of Outer namespace but later one is top-level namespace Commented Jan 27, 2016 at 7:27
  • Possible duplicate of How to implement nested namespace in PHP? Commented Jan 27, 2016 at 9:07

1 Answer 1

1

When you have namespace \Outer in a class, then use Inner is going to be using Inner top-level namespace. If you want to use subnamespace you should use \Outer\Inner

As stated in php using namespaces

Lets say your first file was:

<?php
namespace Outer\Inner;

<?php
 namespace Outer;

/* Qualified name */
Inner\foo(); // resolves to function Outer\Inner\foo    
Sign up to request clarification or add additional context in comments.

Comments

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.