PHP static variables in double quotes

后端 未结 9 1521
礼貌的吻别
礼貌的吻别 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:30

    I don’t know the answer to your question, but you can show the class name and method using the __METHOD__ magic constant.

    0 讨论(0)
  • 2020-11-29 08:36
    <?php
    
    class test {
        public $static = 'text';
        public $self = __CLASS__;
        // static Method
        static function author() {
            return "Frank Glück";
        }
        // static variable
        static $url = 'https://www.dozent.net';
        public function dothis() {
           $self = __CLASS__;
           echo <<<TEST
               
               {${!${''}=static::author()}} // works
               {$self::author()}            // works
               {$this->self::author()}      // works
               ${!${''}=self::author()}     // works
               
               {${$this->self}}::author()}} // don't works
               ${${self::author()}}         // do/don't works but with notice
               ${@${self::author()}}        // works but with @ !
               
    TEST;
        }
    }
    
    $test = 'test'; // this is the trick, put the Classname into a variable
    
    echo "{$test::author()} {$$test::$url}";
    echo <<<HTML
    <div>{$test::author()}</div>
    <div>{$$test::$url}</div>
    HTML;
    
    $test = new test();
    $test->dothis();
    
    0 讨论(0)
  • 2020-11-29 08:37

    Just live with the concatenation. You'd be surprised how inefficient variable interpolation in strings can be.

    And while this could fall under the umbrella of pre-optimization or micro-optimization, I just don't think you actually gain any elegance in this example.

    Personally, if I'm gonna make a tiny optimization of one or the other, and my choices are "faster" and "easier to type" - I'm gonna choose "faster". Because you only type it a few times, but it's probably going to execute thousands of times.

    0 讨论(0)
  • 2020-11-29 08:38

    Yes this can be done:

    log("{${self::$CLASS}} $METHOD entering");
    
    0 讨论(0)
  • 2020-11-29 08:45

    I know this is an old question but I find it odd that noone has suggested the [sprintf][1] function yet.

    say:

    <?php
    
    class Foo {
    
        public static $a = 'apple';
    
    }
    

    you would use it with:

    echo sprintf( '$a value is %s', Foo::$a );
    

    so on your example its:

    log(
        sprintf ( ' %s $METHOD entering', self::$CLASS )
    );
    
    0 讨论(0)
  • 2020-11-29 08:50

    Use an anonymous identity function stored in a variable. This way you will have $ immediately after {:

    $I = function($v) { return $v; }; $interpolated = "Doing {$I(self::FOO)} with {$I(self::BAR)}";

    (I am using class constants in this example but this will work with static variables too).

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