像变量一样在字符串中扩展PHP函数

cha*_*umQ 3 php string function

在PHP中,我可以说

$noun = "bird";
$adjective = "warm;
echo <<<EOT
The $noun is very $adjective
EOT;
Run Code Online (Sandbox Code Playgroud)

它会输出

这只鸟非常温暖

有没有办法用函数做到这一点?

function getNoun() { return "bird"; }
function getAdjective() { return "warm"; }
echo <<<EOT
The getNoun() is very getAdjective()
EOT;
Run Code Online (Sandbox Code Playgroud)

和输出

这只鸟非常温暖

Mar*_*c B 5

你可以使用变量函数,虽然它们相当皱眉,因为它们与变量变量差别不大......

$a = 'getNoun';
$b = 'getAdjective';
echo <<<EOT
The {$a()} is very {$b()}
EOT;
Run Code Online (Sandbox Code Playgroud)