双引号中的 PHP 静态属性

发布于 2024-07-30 18:37:58 字数 279 浏览 3 评论 0原文

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

我想做这样的事情:

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

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

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

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");

I've tried all sorts of {} combos to get the variable value of self::$CLASS, but nothing has worked. I've currently settled with string concatenation but it is a pain to type:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

无人问我粥可暖 2024-08-06 18:37:58

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

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

再可℃爱ぅ一点好了 2024-08-06 18:37:58

不幸的是,目前还没有办法做到这一点。 这里的答案之一的示例将不起作用,因为 {${self::$CLASS}} 不会返回 self::$CLASS 的内容,但会返回内容名称为 self::$CLASS 的变量。

下面是一个示例,它不返回 myvar,而是返回 aaa

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

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}}";
一场信仰旅途 2024-08-06 18:37:58

使用存储在变量中的匿名标识函数。 这样,您将在 {: 之后立即看到 $:(

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

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

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).

沉默的熊 2024-08-06 18:37:58

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

$self = self::class;

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

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

The way I've accomplished it is by setting a variable to the static class itself:

$self = self::class;

Then I can interpolate it:

$string = "This is: {$self::property}";
少女七分熟 2024-08-06 18:37:58
<?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 @ !
<?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();

Output for 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 @ !

Output for 8.1.0 - 8.1.27 (and older)

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 @ !
骄兵必败 2024-08-06 18:37:58

我觉得奇怪的是没有人建议 sprintf 功能还没有。

说:

<?php

class Foo {
    
    public static $a = 'apple';

}

你会使用它:

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

所以在你的例子中它是:

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

I find it odd that noone has suggested the sprintf 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 )
);
相对绾红妆 2024-08-06 18:37:58
//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)";
//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)";
迷迭香的记忆 2024-08-06 18:37:58

是的,可以这样做:

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

Yes this can be done:

log("{${self::$CLASS}} $METHOD entering");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文