Getting static property from a class with dynamic class name in PHP

后端 未结 11 513
情歌与酒
情歌与酒 2020-11-30 01:44

I have this:

  • one string variable which holds the class name ($classname)
  • one string variable with holds the property name ($propert
11条回答
  •  旧时难觅i
    2020-11-30 02:07

    You can use ReflectionClass:

    class foo
    {
        private static $bar = "something";
    }
    
    $class = "foo";
    $reflector = new ReflectionClass($class);
    $static_vars = $reflector->getStaticProperties();
    var_dump($static_vars["bar"]);
    

提交回复
热议问题