在函数内部使用 heredoc 会更改其下方代码的语法突出显示

Using heredoc inside a function changes syntax highlighting of code below it

我在函数内部使用 <<<heredoc heredoc;(在 class 中),它弄乱了它下面所有代码的语法突出显示。

在函数外使用它很好 - 或者在函数内的一行上使用它:

但是如果我在一个函数中使用它(不是在一行中),它​​会弄乱它下面的突出显示,而且我的编辑器(在 Atom 或 Sublime Text 中也是如此)似乎认为它与函数和 class.. 发生了什么?

<?php

class SimpleCMS {
    var $host = 'localhost';
    var $username = 'root';
    var $password = '';
    var $table = '';

    public function display_public() {

    }

    public function display_admin() {
        return <<<ADMIN_FORM 
        ADMIN_FORM;
    }

    public function write() {

    }

    public function connect() {
        mysql_connect($this->host, $this->username, $this->password) or die('Could not connect to the database. ' . mysql_error());
        mysql_select_db($this->table) or die('Could not select database. ' . mysql_error())

        // build the database
        return $this->buildDB();
    }

    private function buildDB() {
        $sql = <<<MySQL_QUERY CREATE TABLE IF NOT EXISTS testDB (title VARCHAR(150), bodyText TEXT, created VARCHAR(100)) MySQL_QUERY;

        return mysql_query($sql);
    }
}

<<<ADMIN_FORM

ADMIN_FORM;
?>

您的 heredoc 终止符需要在最左边的列中,即没有缩进。这记录在 PHP 的网站上:http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

The closing identifier must begin in the first column of the line.

It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including macOS. The closing delimiter must also be followed by a newline.

更改您当前的代码:

    public function display_admin() {
        return <<<ADMIN_FORM
        ADMIN_FORM;
    }

为此:

    public function display_admin() {
        return <<<ADMIN_FORM
ADMIN_FORM;
    }