Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access static member of a class?

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'

like image 444
KoolKabin Avatar asked Sep 29 '10 02:09

KoolKabin


People also ask

How do I access static data members?

We can access the static member function using the class name or class' objects. If the static member function accesses any non-static data member or non-static member function, it throws an error. Here, the class_name is the name of the class. function_name: The function name is the name of the static member function.

How will you access static member of a class in Java?

As discussed above, Any static member can be accessed before any objects of its class are created, and without reference to any object. Methods declared as static have several restrictions: They can only directly call other static methods. They can only directly access static data.

Can you access static members from an instance of a class?

The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.

How do you access a static class object?

The static keyword is used before the class keyword in a class definition to declare a static class. A static class members are accessed by the class name followed by the member name.


2 Answers

If A is a class, you can access it directly via 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 objects or class literals could 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 ($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,

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.

In short, once we have more information as to what you would like to do, we can then go on and provide better answers.

like image 57
Anthony Forloney Avatar answered Oct 09 '22 02:10

Anthony Forloney


If you want a working version for PHP5.2, you can use reflection to access the static property of a class.

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

like image 24
HoLyVieR Avatar answered Oct 09 '22 02:10

HoLyVieR