Class_Name::$_static_property

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guillermobytes
    New Member
    • Jan 2010
    • 77

    Class_Name::$_static_property

    Hi,
    sorry for the title the problem is hard to describe in so few characters.

    i need to be able to get a static class property from a dynamic class name:

    i have a class name stored as a string:
    Code:
    $className = 'My_Class_Name';
    and i need to call a static property of that class, normally i would do:
    Code:
    My_Class_Name::$staticProperty;
    but the class name changes dynamically, so i cannot hard type it like here up. so i need to do something like this:
    Code:
    $className::$staticProperty;
    is this correct or will it crash?

    thanks!
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    it will crash. syntax error:

    But you can use eval(), like so:

    Code:
    
    class foo
    {
        public static $bar = 'It Works!'; 
    }
    
    $my_class = 'foo'; 
    $my_static= 'bar';
    
    echo eval("return $my_class::\$$my_static;");
    The output is:
    Code:
    It Works!
    Cheers,


    Dan

    Comment

    • zorgi
      Recognized Expert Contributor
      • Mar 2008
      • 431

      #3
      From PHP Version 5.3 it should not crash and you can do what guillermobytes suggests.

      Comment

      Working...