[英]How do I access static variables within a class without class names in PHP?
Is there any way to access a static variable of a PHP class without using the class prefix? 有没有办法在不使用类前缀的情况下访问PHP类的静态变量?
Example: 例:
class A {
protected static $x = "blahblah";
public static function p() {
print(A::$x); // <= Is there no way to omit A:: ???
}
}
I understand, that at that point $x could be an uninitialized local variable, but it would make a lot of sense to be able to say/declare/set that in these cases the interpreter should resolve $x as the member declared earlier. 我理解,那时$ x可能是一个未初始化的局部变量,但是能够说/声明/设置在这些情况下解释器应解析$ x作为前面声明的成员会很有意义。 Typing the class prefix everywhere is a major fail IMHO.
在任何地方键入类前缀是一个主要的失败恕我直言。
If the method you're using it from is in the same class, you can use self:: or static::. 如果您使用它的方法在同一个类中,则可以使用self ::或static ::。
Eg: 例如:
class A {
protected static $x = "blahblah";
public static function p() {
print(self::$x);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.