Reputation: 1262
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");
Upvotes: 49
Views: 26290
Reputation: 920
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 )
);
Upvotes: 2
Reputation: 3175
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).
Upvotes: 7
Reputation: 2041
<?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 @ !
Upvotes: 1
Reputation: 13462
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}";
Upvotes: 1
Reputation: 21
//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)";
Upvotes: 0
Reputation: 616
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}}";
Upvotes: 10