php 如何访问类的静态成员?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3818333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access static member of a class?
提问by KoolKabin
I am trying to access static member of a class.
我正在尝试访问类的静态成员。
my class is:
我的课是:
class A
{
public static $strName = 'A is my name'
public function xyz()
{
..
}
..
}
//Since I have bunch of classes stored in an array
$x = array('A');
echo $x::$strName;
I am getting error while printing. How can I print 'A is my name'
我在打印时出错。如何打印“A是我的名字”
回答by Anthony Forloney
If A
is a class, you can access it directly via A::$strName
.
如果A
是一个类,可以直接通过A::$strName
.
class A {
public static $strName = 'A is my name';
}
echo A::$strName; // outputs "A is my name"
Update:
更新:
Depending on what you have inside your array, whether its what I like to define as class objectsor class literalscould be a factor. I distinguish these two terms by,
根据您在数组中拥有的内容,我喜欢将其定义为类对象还是类文字可能是一个因素。我通过以下方式区分这两个术语,
$objClasses = array(new A(), new B()); // class objects
$myClasses = array('A','B'); // class literals
If you go the class literals approach, then using a foreach
loop with PHP5.2.8 I am given a syntax error when using the scope resolution operator.
如果您采用类文字方法,然后foreach
在 PHP5.2.8 中使用循环,则在使用范围解析运算符时会出现语法错误。
foreach ($myClasses as $class) {
echo $class::$strName;
//syntax error, unexpected '::', expecting ',' or ';'
}
So then I thought about using the class objects approach, but the only way I could actually output the static variable was with an instance of an object and using the self
keyword like so,
然后我考虑使用类对象方法,但我实际输出静态变量的唯一方法是使用对象的实例并使用self
像这样的关键字,
class A {
public static $strName = 'A is my name';
function getStatic() {
return self::$strName;
}
}
class B {
public static $strName = 'B is my name';
function getStatic() {
return self::$strName;
}
}
And then invoke that method when iterating,
然后在迭代时调用该方法,
foreach($objClasses as $obj) {
echo $obj->getStatic();
}
Which at that point why declare the variable static
at all? It defeats the whole idea of accessing a variable without the need to instantiate an object.
那个时候为什么要声明变量static
呢?它违背了无需实例化对象即可访问变量的整个想法。
In short, once we have more information as to what you would like to do, we can then go on and provide better answers.
简而言之,一旦我们获得有关您想做什么的更多信息,我们就可以继续提供更好的答案。
回答by HoLyVieR
If you want a working version for PHP5.2, you can use reflectionto access the static property of a class.
如果您想要 PHP5.2 的工作版本,您可以使用反射来访问类的静态属性。
class A {
static $strName= '123';
}
$lstClass = array('A');
foreach ($lstClass as $value) {
$c = new ReflectionClass($value);
echo $c->getStaticPropertyValue('strName');
}
Demo : http://ideone.com/HFJCW
回答by Jim W.
You have a syntax error with missing semicolon and because it is an array you need to access the index of 0, or else it would be trying to call class 'Array'.
您有一个缺少分号的语法错误,并且因为它是一个数组,您需要访问索引 0,否则它将尝试调用类“Array”。
class A
{
public static $strName = 'A is my name';
public function xyz()
{
// left blank and removed syntax error
}
}
$x = array('A');
echo $x[0]::$strName;
Should fix it.
应该修复它。
UPDATE
更新
If you want to iterate over an array to call a class variable:
如果要遍历数组以调用类变量:
$x = array('A', 'B');
foreach ($x as $class) {
echo $class::$strName;
}
Not sure why you would want that, but there you go. And this has been tested, no errors were thrown, valid response of A is my name
was received.
不知道为什么你会想要那个,但你去了。这已经过测试,没有抛出错误,A is my name
收到了有效的响应。
EDIT
编辑
Apparently this only works under PHP 5.3
显然这仅适用于 PHP 5.3
回答by KoolKabin
I do find next one simple solution but don't know whether its good one or not.
我确实找到了下一个简单的解决方案,但不知道它是否好。
My soln is:
我的解决方法是:
eval('return '.$x[0].'::$strName;');
回答by panjeh
From inside a class and want to access a static data member of its own you can also use static::instead of self::
从类内部并想要访问它自己的静态数据成员,您也可以使用 static::而不是 self::