双引号中的 PHP 静态属性

问题描述 投票:0回答:10

如何让 PHP 计算双引号中的静态变量?

我想做这样的事情:

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

我尝试了各种

{}
组合来获取
self::$CLASS
的变量值,但没有任何效果。我目前已经解决了字符串连接问题,但输入起来很痛苦:

log(self::$CLASS . " $METHOD entering");
php variables static double quotes
10个回答
49
投票

抱歉,你不能这样做。它仅适用于简单的表达式。请参阅此处


9
投票

不幸的是,目前还没有办法做到这一点。这里的答案之一中的示例不起作用,因为

{${self::$CLASS}}
不会返回
self::$CLASS
的内容,但会返回
self::$CLASS
中名称的变量内容。

这里是一个例子,它不返回

myvar
,而是
aaa

$myvar = 'aaa';
self::$CLASS = 'myvar';
echo "{${self::$CLASS}}";

7
投票

使用存储在变量中的匿名身份函数。这样你就会在

$
之后立即得到
{
:

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

(我在本示例中使用类常量,但这也适用于静态变量)。


4
投票

我不知道你问题的答案,但你可以使用

__METHOD__
魔法常量来显示类名和方法。


1
投票

我知道这是一个老问题,但我觉得很奇怪,没有人建议使用

[sprintf][1]
函数。

说:

<?php

class Foo {

    public static $a = 'apple';

}

您可以将其用于:

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

所以在你的例子中:

log(
    sprintf ( ' %s $METHOD entering', self::$CLASS )
);

1
投票
<?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()}      // do/don't works but with notice
           ${!${''}=self::author()}     // works
           
           {${$this->self}}::author()}} // do/don't works but with notice
           ${${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();

8.2.14 - 8.2.17、8.3.0 - 8.3.4 的输出:

Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in /in/uGdhi on line 18

Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in /in/uGdhi on line 21

Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead in /in/uGdhi on line 22
Frank Glück https://www.dozent.net<div>Frank Glück</div>
<div>https://www.dozent.net</div>
Warning: Undefined variable $test in /in/uGdhi on line 21

Warning: Undefined variable $Frank Glück in /in/uGdhi on line 22
           
           Frank Glück // works
           Frank Glück            // works
           Frank Glück      // do/don't works but with notice
           Frank Glück     // works
           
           ::author()}} // do/don't works but with notice
           Frank Glück         // do/don't works but with notice
           Frank Glück        // works but with @ !

8.1.0 - 8.1.27(及更早版本)的输出

Frank Glück https://www.dozent.net<div>Frank Glück</div>
<div>https://www.dozent.net</div>
Warning: Undefined variable $test in /in/uGdhi on line 21

Warning: Undefined variable $Frank Glück in /in/uGdhi on line 22
           
           Frank Glück // works
           Frank Glück            // works
           Frank Glück      // do/don't works but with notice
           Frank Glück     // works
           
           ::author()}} // do/don't works but with notice
           Frank Glück         // do/don't works but with notice
           Frank Glück        // works but with @ !

0
投票
//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
投票

我完成此任务的方法是为静态类本身设置一个变量:

$self = self::class;

然后我可以对其进行插值:

$string = "This is: {$self::property}";

-3
投票

只要接受串联即可。 您会惊讶于字符串中的变量插值是多么低效

虽然这可能属于预优化或微优化的范畴,但我认为您实际上并没有在这个示例中获得任何优雅。

就我个人而言,如果我要对其中之一进行微小的优化,并且我的选择是“更快”和“更容易打字” - 我会选择“更快”。因为你只输入几次,但它可能会执行数千次。


-5
投票

是的,可以这样做:

log("{${self::$CLASS}} $METHOD entering");
© www.soinside.com 2019 - 2024. All rights reserved.