BraedenP
BraedenP

Reputation: 7215

Dynamically Load Class and Access Static Variable in PHP <= 5.2

Edit:

I solved it by getting all of the class variables using get_class_vars(), and then just acquired the correct property from that array, if it existed. Seems simple enough to me; if anyone has a different solution, I'd love to hear it (or read it, I guess..) :)

I'm trying to access a static variable in a dynamically-loaded class as follows:

$file::$disabled

(In the above statement, $file obviously references the name of a class, and $disabled is the static variable I want to access within the class.)

On PHP 5.3, this works fine; as a result of running the above code on lower versions, I get the infamous T_PAAMAYIM_NEKUDOTAYIM error.

How I've usually gotten around this error when working with older versions of PHP is to create a getter function for that variable and get that return value with call_user_func(). However, for ease of use of developers who will be adopting this code, I would like to keep $disabled as a simple variable rather than a function.

I've tried eval() on the statement, only to reach another dead end.

Does anybody know how I can make this happen?

Upvotes: 2

Views: 487

Answers (1)

Artefacto
Artefacto

Reputation: 97835

One option would be to use reflection:

$rp = new ReflectionProperty($file, $disabled);
$value = $rp->getValue();

or

$rc = new ReflectionClass($file);
$value $rc->getStaticPropertyValue($disabled);

Upvotes: 2

Related Questions