PHP:默认情况下,在子命名空间类中使用父命名空间

For example my root namespace is myns where are all helper classes. There is myns\controllers namespace where are all the controllers.

I want to use myns namespace by default in all myns\controllers classes, avoiding many

use myns\classname;

declarations at the beginning.

Is that possible?

No. The "default namespace" is established by namespace, not use. You have two options:

  1. Switch the namespace:

    namespace myns\controllers {
        class Foo {}
    }
    
    namespace myns {
        use myns\controllers\Foo;
    
        new Foo;  // above Foo class
        new Bar;  // myns\Bar class
    }
    
  2. Use a shortened handle:

    use myns as m;
    
    new m\Bar;  // myns\Bar
    

Practically speaking, in sane code, you shouldn't be having a ton of stuff from other namespaces that you're going to use; explicitly aliasing a small handful (say, up to around a dozen) of functions and classes at the top of the file is pretty normal. A decent IDE can pretty much auto-generate those use statements as you type.