views:

1811

answers:

7

Hi Guys,

I have this:

  • one string variable which holds the class name ($classname)
  • one string variable with holds the property name ($propertyname)

I want to get that property from that class, the problem is, the property is static and I don't know how to do that.

If the property weren't static, it would have been:

$classname->$propertyname;

if the property were a method, I could have used call_user_function

call_user_func(array($classname, $propertyname));

But in my case, am I just lost. I am however hoping that it is possible. With the thousands of functions that PHP has, he'd better have something for this as well. Maybe I'm missing something?

Thanks!

Edit:

  • for those with eval() solutions: thanks, but it is out of the question
  • for those with get _class _vars() solutions: thanks, but it seems it returns "the default properties of the given class" (php.net), and yes, I would like that value to be changable (even though it does help me in some of the cases)
+2  A: 

You should be able to do something like:

eval("echo $classname::$propertyname;");

I just did a quick test and got this to work for me. Not sure if there's a better way or not, but I wasn't able to find one.

Steven Surowiec
+12  A: 
Andrew Moore
+1 for wrapper function.
nilamo
Oh my god, I was just about to ask "but how about setting...". Dude, you totally rock!
treznik
In PHP 5 and up (I'm on 5.2), you can use reflection for this, as I just learned. See my answer here: http://stackoverflow.com/questions/3354628/from-the-string-name-of-a-class-can-i-get-a-static-variable/3364090#3364090
Nathan Long
A: 

'eval' looks so close to 'evil', and I hate using it and/or seeing it in code. With a few ideas from other answers, here's a way to avoid it even if your php isn't 5.3 or higher.

Changed to reflect testing based on a comment.

class A {
    static $foo = 'bar';
}

A::$foo = 'baz';
$a = new A;

$class = get_class($a);
$vars = get_class_vars($class);

echo $vars['foo'];

Outputs 'baz'.

nilamo
`get_class_vars()` only returns the initial value of `$foo`. If `$foo` changes, the value you will get from `get_class_vars()` will not change.
Andrew Moore
Actually, by definition it's not supposed to work, but apparently does.
Andrew Moore
I agree with your second comment. I thought that was a little weird, so I tested it and updated my answer. Does that match what you found?
nilamo
Actually, `get_class_vars('A')` just works fine. You don't need to create an instance.
Andrew Moore
Sweet. *<|:^) <-- meeting minimum char limit with Santa.
nilamo
A: 

Potentially relevant: discussion on late static binding in PHP - http://stackoverflow.com/questions/87192/when-would-you-need-to-use-late-static-binding.

Alex
A: 

get_class_vars is not same as get_object_vars i think get_clas_vars should return the original property values

firzen
A: 

One thing I noticed is that you can't set variables which are protected in static classes as the eval() command runs in a scope outside the class. The only thing to get around this would be to implement a static method inside the/every class which runs the eval(). This method could be protected as the call_user_func() [to call the setter method] also runs from inside the class.

mbirth
+2  A: 

I think this is the simplest:

$foo = new ReflectionProperty('myClassName', 'myPropertyName'); 
print $foo->getValue();
Bahadır