0

I have a class like this:

class main {

    private static $content;

    public function ini() {
        include 'content_a.php';
        self::$content = new $content;
    }

    public function content($snippet, $addclass=false) {
        return self::$content->snippet;
    }
}

It parses some input and determines which file to include in ini(), which runs once at the top of each pageload. In this example, it includes content_a.php, but there are more (content_b.php, content_c.php, ...). The method saves the content of the file to the $content property for use by content(), which is called numerous times throughout the rest of the script.

The file content_a.php looks like this:

class content {
    public $text = <<<FOO
    <div class="default $addclass">Hello World</div>
FOO;
}

No matter whether it's a, b, or c, it always looks the same - only the actual content within the heredoc differs. Each file contains of number of snippets; in this example, there's only one ($text).

I'm rendering a snippet like this:

echo $main->content('text');

...which works perfectly. However, when I try to pass through a parameter $addclass like this...

echo $main->content('text', 'bar');

...I'm getting the error unexpected T_VARIABLE, expecting T_END_HEREDOC. If I wrap the variable in curly brackets, I get unexpected T_CURLY_OPEN, expecting T_END_HEREDOC.

Any ideas what I'm doing wrong - or is there a better way to do this?

bobsoap
  • 4,844
  • 7
  • 30
  • 43
  • `$addclass` is local to the `content()` function, how do you expect it to be used by `ini()`? – Barmar Nov 11 '13 at 21:40
  • HamZa, how exactly would my question be a duplicate of a general "how to solve syntax errors" question? Sorry if that was unclear, but I was looking for a way to pass variables from a method into content stored as a heredoc and called from another method. – bobsoap Nov 11 '13 at 23:40
  • I'm also not sure where that box above the question is coming from... "This question may already have an answer here". If you added that, please consider removing it because that's simply wrong. Thanks. – bobsoap Nov 11 '13 at 23:42
  • Barmar, you're right - that was my mistake. `$text` is being parsed in `ini()` and doesn't stay open for modification in `content()`. I added my solution as an answer below if you're interested - it does what I intended to do with this code. – bobsoap Nov 11 '13 at 23:44

2 Answers2

1

Using expressions in class variables is not allowed in PHP, you can use only static content within heredoc when it's assigned to a class variable

so you can use %s instead of $addclass then replace it by using sprintf($this->text,$addClass);

skmail
  • 408
  • 3
  • 6
0

I found the best way to do this for my case. Solaiman's answer would technically work, but I also wanted it to be scalable, i.e. I wanted to be able to pass in different variables for different snippets. One might have an added class while another, e.g. for a link, might have a dynamic href attribute.

So I resorted to doing the following:

  • replace $variable with %%variable%% in $text
  • run a quick str_replace() before returning $text

For the moment, this seems to be the most scalable and flexible. I'm still open to other solutions if someone can offer one, though.

Thanks again Solaiman for your answer.

bobsoap
  • 4,844
  • 7
  • 30
  • 43