php - 'Unexpected T_VARIABLE' (presumed related to HEREDOC) -
this question has answer here:
i have following piece of php, giving me a:
parse error: syntax error, unexpected '$author' (t_variable) in /var/www/html/blog/index.php on line 77
<?php class post { //define placeholders public $title = 'post title'; public $timestamp = 'timestamp'; public $author = 'author'; public $author_url = '/user/'. $author; $post_boilerplate = <<<eof <div class="blog-post"> <h2 class="blog-post-title"> $this->title </h2> <p class="blog-post-meta">$this->timestamp <a href="$this->author_url">$this->author</a></p> <p>blah, blah, blah...</p> </div><!-- /.blog-post --> eof; public function print_post() { echo $this->post_boilerplate; } } $post = new post(); $post->print_post(); ?>
i've tried commenting out referenced line quick 'fix', error moves around.
with
//public $author_url = '/user/'. $author;
i get:
parse error: syntax error, unexpected '$post_boilerplate' (t_variable), expecting function (t_function) in /var/www/html/blog/index.php on line 79
commenting out heredoc 'helps', in containing page gets generated, albeit errors pertaining missing variable declarations, that's not solution.
could shed light on what's wrong?
you can't initialize class variables in way.
one way define constant
define('author',$author); //... public $author_url = '/user/'. author;
a better way initialize values in constructor function.
function __construct() { $this->post_boilerplate = " <div class='blog-post'> <h2 class='blog-post-title'> ".$this->title."</h2> <p class='blog-post-meta'>".$this->timestamp." <a href='".$this->author_url."'>".$this->author."</a></p> <p>blah, blah, blah...</p> </div><!-- /.blog-post --> "; }
Comments
Post a Comment