PHP static variables in double quotes

后端 未结 9 1522
礼貌的吻别
礼貌的吻别 2020-11-29 08:16

How can I get PHP to evaluate a static variable in double quotes?

I want to do something like this:

log("self::$CLASS $METHOD entering");


        
相关标签:
9条回答
  • 2020-11-29 08:50
    //define below
    function EXPR($v) { return $v; }
    $E = EXPR;
    
    //now you can use it in string
    echo "hello - three is equal to $E(1+2)";
    
    0 讨论(0)
  • 2020-11-29 08:52

    Unfortunately there is no way how to do this yet. Example in one of answers here will not work, because {${self::$CLASS}} will not returns content of self::$CLASS, but will returns content of variable with name in self::$CLASS.

    Here is an example, which does not returns myvar, but aaa:

    $myvar = 'aaa';
    self::$CLASS = 'myvar';
    echo "{${self::$CLASS}}";
    
    0 讨论(0)
  • 2020-11-29 08:53

    Sorry, you can't do that. It only works for simple expressions. See here.

    0 讨论(0)
提交回复
热议问题