Access variable in class
i would like to access an v开发者_运维问答ariable, which is in an class (not as an instance of an class) For example
class myclas
{
private $list=array('1','2','3');
[...]
}
I need to access the values of $list in that way: myclass::$list (witch is'n possible). Is there an alternative way?
Thank you.
//Edit: Thank you all for the answer! Is it possible to use an private variable as values for an public?
class myclas
{
private $_list=array('1','2','3');
public static $staticList=$_list;
[...]
}
Right now, i get an error "unexpected T_VARIABLE"
class myclas
{
public static $list=array('1','2','3');
}
myClass::$list;
See this beautiful guide: http://php.net/manual/en/language.variables.scope.php
It's a private variable. If you made it a public static variable you should be able to access it:
class myclas {
public static $list = array('1','2','3');
}
myclas::$list;
It needs to be declared as static.
Example:
class MyClass {
public static $var = 'foo';
}
Then to access: MyClass::$var;
For your edit, see this other beatiful guide about classes and visibility:
http://www.php.net/manual/en/language.oop5.visibility.php
精彩评论