假设我的班级中有一个名为$_staticVar的静态变量,我试图像这样访问.该变量有一个成员aString,其字符串值为“my static variable”
echo <<<eos
<br/>This is the content of my static variable,
self::$_staticVar->$aString
which is not getting accessed properly in heredoc syntax. <br/>
eos;
输出:
Notice: Undefined variable: _staticVar in /path/to/file.php on line some_line_number
<br/>This is the content of my static variable,
self::->my static variable,
which is not getting accessed properly in heredoc syntax.<br/>
我试过这个:
echo <<<eos
<br/>This is the content of my static variable,<br/>
{${self::$_staticVar->$aString}}<br/>
which is not getting accessed properly in heredoc syntax. <br/>
eos;
它不起作用.
输出:
Notice: Undefined variable: _staticVar in /path/to/file.php on line some_line_number
<br/>This is the content of my static variable,
which is not getting accessed properly in heredoc syntax.<br/>
这是我的PHP设置:
display_startup_errors = on
display_errors = On
error_reporting = E_ALL | E_STRICT
解决方法:
我很确定你必须使用本地或导入的变量进行字符串插值.最简单的解决方案?为什么,当然是当地的:
$_staticVar = self::$_staticVar; // or did you mean self::_staticVar? Not too clear on that.
echo <<<eos
<br/>Something {$_staticVar->something} more of something <br/>
eos;
至于你的例子不起作用的原因:
echo <<<eos
<br/>Something self::$_staticVar->{$something} more of something <br/>
eos;
插入未定义的变量$something和$_staticVar,这会产生一个空字符串和一个通知.
echo <<<eos
<br/>Something {${self::$$_staticVar->{$something}}} more of something <br/>
eos;
插值绝对不存在但永远不会存在的东西,这一切都让人感到困惑,但你知道它不起作用.