PHP 在另一个不带反斜杠的命名空间中使用根命名空间
PHP use root namespace within another namespace without backslash
我的根命名空间中有一堆 类。我想在另一个命名空间中使用它们,但我不知道如何 "include" 它们,这样我就不必在开头添加反斜杠。
class A {
public static $a = 1;
}
namespace B {
use \; // apparently invalid
class C {
static function D { return A::$a; } // desired syntax
}
}
\B\C::D(); // expected result is 1
这可能吗?
将您的 use
语句更改为
use \A
-
然后你会得到你想要的语法 -
无论命名空间是否为"root",您都无法使用use
导入整个命名空间。你能做的最好的是:
use A;
我的根命名空间中有一堆 类。我想在另一个命名空间中使用它们,但我不知道如何 "include" 它们,这样我就不必在开头添加反斜杠。
class A {
public static $a = 1;
}
namespace B {
use \; // apparently invalid
class C {
static function D { return A::$a; } // desired syntax
}
}
\B\C::D(); // expected result is 1
这可能吗?
将您的 use
语句更改为
use \A
-
然后你会得到你想要的语法 -
无论命名空间是否为"root",您都无法使用use
导入整个命名空间。你能做的最好的是:
use A;