views:

110

answers:

4

Hi guys,

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'

+3  A: 

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.

Anthony Forloney
well i have a list of class in a array which i have to iterate throug so do have to access using array
KoolKabin
I am not quite following, what is it that you are trying to do?
Anthony Forloney
Looks right. See http://www.php.net/manual/en/language.oop5.static.php. This is what I was trying to remember.
James McLeod
I even tried the example given in but its giving me error. I got PHP version 5.2
KoolKabin
@KoolKabin I am using PHP 5.2.8 and it ran fine on my browser. Can you actually post what you are trying to do? It seems what you are trying to do is a bit messy and with some more information could be cleaned up a bit. Do you have an array of classes?, ie. `array(new A(), new B())`
Anthony Forloney
+2  A: 

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'.

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.

EDIT

Apparently this only works under PHP 5.3

Brad F Jacobs
This was directed @ a deleted comment as an fyi: I tested it via the `php -a` cli tool. It worked great. I did modify the code to remove the `...` as they would need to be removed to not cause syntax errors. And the `B` class will need to be created for the **UPDATE** section to work properly.
Brad F Jacobs
@premiso Nevermind ideone.com is using the PHP5.2 and it works fine under PHP5.3
HoLyVieR
its not working on my php ver 5.2 :( what can be my solution
KoolKabin
@KoolKabin see my answer for a working version for PHP5.2.
HoLyVieR
+2  A: 

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

HoLyVieR
A: 

Hi guys,

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;');
KoolKabin
@KoolKabin, I wouldn't rely on `eval` much, it can be evil.
Anthony Forloney
can it be treated as evil if we are going to use only the fixed predefined commands
KoolKabin
@KoolKabin Honestly, as a personal preference I wouldn't even use it with predefined commands. No, it shouldn't be *evil* to do it the way you are doing it, but I know there are better solutions.
Anthony Forloney
@Kool yes, it is extremely problematic to use that. For example, if the class is not found, this will just create a fatal error, which will make the hole script stop without any possibility of catching the error. If you were using reflection, it will launch an exception that can be catched.
HoLyVieR
thnx for suggestion. so then reflection method is the better one than others. is it?
KoolKabin