Home » Php » php – Heredoc syntax error

php – Heredoc syntax error

Posted by: admin July 12, 2020 Leave a comment

Questions:

For a mediawiki skin, I am getting an error for the following piece of code

    private $_sideboxf = <<<EOD
        <h3>%s</h3>
        <ul class="submenu">
            %s
        </ul>
EOD;

The error is syntax error, unexpected T_START_HEREDOC . I have checked the syntax and I dont know what is wrong. Even the author of the code doesn’t understand why I am getting this error when I asked on git.

EDIT: class definition

class SkinTemplate extends QuickTemplate
{
    private $_sideboxf = <<<EOD
    <h3>%s</h3>
    <ul class="submenu">
        %s
    </ul>
EOD;
// Some public function
} // End of Class
How to&Answers:

PHP5.3.0+ is required for initializing class-variables using heredoc, your code works fine in PHP 5.3

Answer:

You really want to do that heredoc during member variable initialization? Maybe a better route to take would be something like:

class MyClass 
{
    public function getSideBoxF()
    {
        return <<<EOD
            Stuff goes here...
EOD;

    }
}

Also note that the closing HEREDOC must be flush against the left margin, I added the class wrapper to show you what it should look like.

Answer:

Is the end tag of the heredoc actually indented?

That doesn’t work out well IIRC…