意外的'"';在[..]中


Unexpected ' " ' in [...]

在一个类中,我在第22行得到了一个"Unexpected"'[…]",我一辈子都不知道为什么。

错误在的第22行

<?php
namespace oowp'post;
global $wpdb;
class PostObjectQueryBuilder extends QueryBuilder implements IExtendable {
    use Extendable;
    // WP Tables
    private $tablePosts;             //The table of Posts.
    private $tablePostmeta;          //The Meta Content (a.k.a. Custom Fields) table.
    private $tableComments;          //The Comments table.
    private $tableCommentmeta;       //The table contains additional comment information.
    private $tableTerms;             //The terms table contains the 'description' of Categories, Link Categories, Tags.
    private $tableTermTaxonomy;      //The term_taxonomy table describes the various taxonomies (classes of terms). Categories, Link Categories, and Tags are taxonomies.
    private $tableTermRelationships; //The term relationships table contains link between the term and the object that uses that term, meaning this file point to each Category used for each Post.
    private $tableUsers;             //The table of Users.
    private $tableUsermeta;          //The usermeta table contains additional user information, such as nicknames, descriptions and permissions.
    private $tableLinks;             //The table of Links.
    private $tableOptions;           //The Options table.
    // LINE 22 BELOW
    const BASE_QUERY = "SELECT wp.ID FROM `{$this->tablePosts}` wp
        INNER JOIN `{$this->tablePostMeta}` wm ON (wm.`post_id` = wp.`ID`)
        INNER JOIN `{$this->tableTermRelationships}` wtr ON (wp.`ID` = wtr.`object_id`)
        INNER JOIN `{$this->tableTermTaxonomy}` wtt ON (wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`)
        INNER JOIN `{$this->tableTerms}` wt ON (wt.`term_id` = wtt.`term_id`)";

我尝试过重写有问题的行,并尝试将const声明移到顶部,同时删除use语句。两者都不起作用。

这是因为类常量需要有一个常数值,而您的定义不是常量。

来自类常量手册:

该值必须是常量表达式,而不是(例如)变量,属性或函数调用。

您可以通过移除const并将BASE_QUERY转换为$BASE_QUERY并移除$this->作为其他变量来证明您的语法在类外是正确的,它应该会成功解析。

您可以将此表达式定义为成员变量,但不能在初始化期间定义。这是因为PHP中的成员变量也必须初始化为常量表达式。最简单的解决方案是在类中定义$BASE_QUERY,然后在构造函数中初始化它。

我不明白为什么要创建一个常量来获得基本查询。

表达式不允许作为类常数值

有一个返回基本查询的函数很简单,你可以在任何你想使用它的地方调用它

public function getBaseQuery()
{
    return "SELECT wp.ID FROM `{$this->tablePosts}` wp
    INNER JOIN `{$this->tablePostMeta}` wm ON (wm.`post_id` = wp.`ID`)
    INNER JOIN `{$this->tableTermRelationships}` wtr ON (wp.`ID` = wtr.`object_id`)
    INNER JOIN `{$this->tableTermTaxonomy}` wtt ON (wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id`)
    INNER JOIN `{$this->tableTerms}` wt ON (wt.`term_id` = wtt.`term_id`)";
}