Dyanmically Call Static Member

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Is it possible to dyanmically call a classes static member or property?
So something like

class test1 {
    static $staticVar = "Help";

$className = "test1";
echo $className::$staticVar;

This produces an error. Does anyone know if its possible?  Thanks

Re: Dyanmically Call Static Member

This is a known bug / limitation in PHP.  See

Jon Tjemsland

Re: Dyanmically Call Static Member

Found a work around to dyanmically calling a classes static member.
Try the following with eval:

$className = "test1";
eval('$instance = ' . $className . '::$staticVar;');
echo "test: $instance";

Let me know how it goes.

Jon Tjemsland

Site Timeline