doumixiang2227 2013-12-06 04:58
浏览 53
已采纳

PHP中的对象内的heredoc变量

The following php code will throw an error:

class Foo{
    public $bar = <<<EOF
         {$this->foobar}
EOF;
    public $foobar = "123";

   public function echoBar(){
         echo $this->bar;
   }
}

It doesn't even have to be in brackets, any attempt at variable substitution throws an error.

Can someone explain why? Putting the heredoc inside a function fixes the error, but I was wondering what causes this.

Version of php shell is Zend Engine v2.3.0.

  • 写回答

1条回答 默认 最新

  • douchuang4181 2013-12-06 05:02
    关注

    The answer is here in the official PHP docs:

    Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?