-1

I have the following piece of PHP, which is 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 by <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 the referenced line as a quick 'fix', but then the error just 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 the HEREDOC too 'helps', in that the containing page gets generated, obviously albeit with errors pertaining to missing variable declarations, but obviously that's not actually solution.

Could anyone shed some light on what's wrong?

Ansel Pol
  • 19
  • 2
  • Heredoc text should not be indented. – Jay Blanchard Jun 13 '16 at 00:43
  • From things I've read, sych as http://stackoverflow.com/questions/4068556/php-using-proper-indentation-with-heredocs I get the idea that it's only the closing delimiter which must exist on a line by itself. Anyway, removing the indentation didn't fix the problem: I'm still getting the first error I quoted. – Ansel Pol Jun 18 '16 at 21:00

1 Answers1

1

You can't initialize class variables in that way.

One way would be to define the constant

define('AUTHOR',$author);

//...

public $author_url = '/user/'. AUTHOR;

A better way is to initialize the values in a 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." by <a href='".$this->author_url."'>".$this->author."</a></p>

    <p>Blah, blah, blah...</p>
  </div><!-- /.blog-post -->
  ";
}
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • That's still a constant scalar expression; which would remain a syntax error up to PHP 5.5. – mario Jun 13 '16 at 00:43
  • @mario thanks for the tip! I didn't know that. OP must be at least that current or they would've gotten an error at `$title` – Jeff Puckett Jun 13 '16 at 00:48
  • Would have stumbled over the heredoc indeed. So the constructor approach is really the only option here. – mario Jun 13 '16 at 00:59
  • I was intending to shift the assignment of the variables into a constructor once I'd got the basic structure (my OO is a bit rusty). However, using `define('AUTHOR',$author);` I get `Parse error: syntax error, unexpected 'define' (T_STRING), expecting function `(T_FUNCTION) in /var/www/html/blog/index.php on line 76` @mario: What's a constant scalar expression, and why would it have stumbled over the heredoc 'indeed'? Also, regarding versions, I'm running 5.6.20. – Ansel Pol Jun 18 '16 at 21:09
  • @Ansel you can't define constants inside the class. – Jeff Puckett Jun 18 '16 at 21:32