如何在PHP Heredoc语法中显示数组元素或对象属性的值

Wil*_*gan 4 php arrays object heredoc

我在heredoc输入字段中显示数组值时遇到问题.这是一段代码:

class Snippet{
  protected $_user;
  protected $_class;
  protected $_messages;

  public function __construct(){
    $this->_user = $this->_class = NULL;
    $this->createUser();
  }

  public function createUser(){
    $keys = array('user_login','user_email');
    $this->_user = $this->_class = array_fill_keys($keys, '');
  }

  public function userErrors(){
    //by default give the login field autofocus
    $_class['user_login'] = 'autofocus';
  }


  public function getForm(){

  return <<<HTML
<form id='test' action='' method='post'>
$this->_messages    
  <fieldset>
    <legend>Testing Heredoc</legend>
    <ol>
      <li>
        <label for='user_login'>Username</label>
        <input  id='user_login' name='user_login' type='text' placeholder='Login name' value=$this->_user[user_login] $this->_class[user_login]>
      </li>
    </ol>
  </fieldset>
</form>
HTML;
  }
}
Run Code Online (Sandbox Code Playgroud)

我的输出产生一个带标签的输入框,其值显示为Array[user_login].我可以将数组分配给单独的变量并获取我正在寻找的输出,但这似乎是一种不必要的浪费.PHP 5.3.5

Kon*_*ole 10

{}像这样将数组变量包装在花括号内

value="{$this->_user['user_login']} {$this->_class['user_login']}"
Run Code Online (Sandbox Code Playgroud)

是一篇很好的文章.

来自文章

不幸的是,PHP没有提供任何直接的方法来调用函数或在HEREDOC字符串中输出表达式结果.这里的问题是,除非花括号中的东西以美元符号开头,否则PHP无法将其识别为要替换的东西.