PHP: using sub-namespace class inside a namespace

775 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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