简体   繁体   English

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

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

I am using <<<heredoc heredoc; 我正在使用<<<heredoc heredoc; inside a function (in a class) and it messes up the syntax highlighting of all the code below it. 在一个函数内部(在一个类中)它会混淆下面所有代码的语法高亮。

It's fine using it outside the function - or on a single line within a function: 可以在函数外部使用它 - 或者在函数内的单行上使用它:

PHP

But if I use it in a function (not on a single line), it messes up the highlighting below it, and my editor (same in Atom or Sublime Text) seems to think it closes with the one outside the function and class.. what's happening? 但是如果我在一个函数中使用它(不是在单行上),它会混淆它下面的突出显示,而我的编辑器(在Atom或Sublime Text中也是如此)似乎认为它与函数和类之外的那个关闭。发生了什么?

PHP

<?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;
?>

Your heredoc terminator needs to be in the leftmost column, ie without indentation. 你的heredoc终止符需要在最左边的列中,即没有缩进。 This is documented on PHP's website: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.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. 这是在UNIX系统上的\\ n,包括macOS。 The closing delimiter must also be followed by a newline. 结束分隔符也必须后跟换行符。

Change your current code: 更改您当前的代码:

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

To this: 对此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM