从PHP中的动态类名获取静态属性 [英] Getting static property from a class with dynamic class name in PHP

查看:337
本文介绍了从PHP中的动态类名获取静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:




  • 一个持有类名的字符串变量( $ classname
  • 一个带有属性名称的字符串变量( $ propertyname



    • 我想从该类获取该属性,问题是属性是静态的,我不知道该怎么做。



      如果属性不是静态的,那么它将是:

        $ classname-> $ PROPERTYNAME; 

      如果属性是一种方法,我可以使用call_user_function

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

      但在我的情况下,我只是迷路了。不过,我希望这是可能的。随着PHP拥有数以千计的功能,他最好也有一些这样的东西。也许我错过了一些东西?



      谢谢!



      编辑:


      $对于 eval()的用户,b $ b

      解决方案

      如果您使用的是PHP 5.3.0或更高版本,则可以使用以下内容:

        $ classname :: $$ PROPERTYNAME; 


      不幸的是,如果您使用的版本低于5.3.0,您使用 eval() get_class_vars() 将不起作用

        $ value = eval($ classname。'$ $。$ propertyname。';'); 






      编辑:我刚刚说过 get_class_vars() 将不起作用,如果值是动态的,但显然,变量静态成员是类的默认属性。您可以使用以下包装:

        function get_user_prop($ className,$ property){
      if(!class_exists $ className))return null;
      if(!property_exists($ className,$ property))return null;

      $ vars = get_class_vars($ className);
      return $ vars [$ property];
      }

      class Foo {static $ bar ='Fizz'; }

      echo get_user_prop('Foo','bar'); // echoes Fizz
      Foo :: $ bar ='Buzz';
      echo get_user_prop('Foo','bar'); // echo be Buzz

      不幸的是,如果要设置变量的值,您仍然需要使用 eval() ,但通过一些验证,

       函数set_user_prop($ className,$ property,$ value){ 
      if(!class_exists($ className))return false;
      if(!property_exists($ className,$ property))return false;

      / *由于我不能相信$ value的价值
      *我把它放在单引号(我不是
      *想要它的值被评估现在它将
      *仅作为变量引用解析)。
      * /
      eval($ className。':: $'。$ property。'= $ value;');
      返回true;
      }

      class Foo {static $ bar ='Fizz'; }

      echo get_user_prop('Foo','bar'); // echoes Fizz
      set_user_prop('Foo','bar','Buzz');
      echo get_user_prop('Foo','bar'); //回显Buzz

      set_user_prop() with这个验证应该是安全的。如果人们开始将随机的东西作为 $ className $ property ,它将退出该函数,因为它赢得'成为现有的课堂或财产。从 $ value ,它从来没有实际解析为代码,所以不管它们放在哪里都不会影响脚本。


      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)

      解决方案

      If you are using PHP 5.3.0 or greater, you can use the following:

      $classname::$$propertyname;
      

      Unfortunately, if you are using a version lower than 5.3.0, you are stuck using eval() (get_class_vars() will not work if the value is dynamic).

      $value = eval($classname.'::$'.$propertyname.';');
      


      EDIT: I've just said get_class_vars() wouldn't work if the value is dynamic, but apparently, variable static members are part of "the default properties of a class". You could use the following wrapper:

      function get_user_prop($className, $property) {
        if(!class_exists($className)) return null;
        if(!property_exists($className, $property)) return null;
      
        $vars = get_class_vars($className);
        return $vars[$property];
      }
      
      class Foo { static $bar = 'Fizz'; }
      
      echo get_user_prop('Foo', 'bar'); // echoes Fizz
      Foo::$bar = 'Buzz';
      echo get_user_prop('Foo', 'bar'); // echoes Buzz
      

      Unfortunately, if you want to set the value of the variable, you will still need to use eval(), but with some validation in place, it's not so evil.

      function set_user_prop($className, $property,$value) {
        if(!class_exists($className)) return false;
        if(!property_exists($className, $property)) return false;
      
        /* Since I cannot trust the value of $value
         * I am putting it in single quotes (I don't
         * want its value to be evaled. Now it will
         * just be parsed as a variable reference).
         */
        eval($className.'::$'.$property.'=$value;');
        return true;
      }
      
      class Foo { static $bar = 'Fizz'; }
      
      echo get_user_prop('Foo', 'bar'); // echoes Fizz
      set_user_prop('Foo', 'bar', 'Buzz');
      echo get_user_prop('Foo', 'bar'); // echoes Buzz
      

      set_user_prop() with this validation should be secure. If people start putting random things as $className and $property, it will exit out of the function as it won't be an existing class or property. As of $value, it is never actually parsed as code so whatever they put in there won't affect the script.

      这篇关于从PHP中的动态类名获取静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆