Access variable in class

后端 未结 5 945
孤独总比滥情好
孤独总比滥情好 2021-01-26 01:03

i would like to access an variable, which is in an class (not as an instance of an class) For example

class myclas
{
private $list=array(\'1\',\'2\',\'3\');
[...         


        
相关标签:
5条回答
  • 2021-01-26 01:43

    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;
    
    0 讨论(0)
  • 2021-01-26 01:46

    See this beautiful guide: http://php.net/manual/en/language.variables.scope.php

    0 讨论(0)
  • 2021-01-26 01:56

    For your edit, see this other beatiful guide about classes and visibility:

    http://www.php.net/manual/en/language.oop5.visibility.php

    0 讨论(0)
  • 2021-01-26 01:57
    class myclas
    {
    public static $list=array('1','2','3');
    }
    myClass::$list;
    
    0 讨论(0)
  • 2021-01-26 01:58

    It needs to be declared as static.

    Example:

    class MyClass {
    
        public static $var = 'foo';
    }
    

    Then to access: MyClass::$var;

    0 讨论(0)
提交回复
热议问题