在[...]中出乎意料的'''

时间:2015-12-18 19:24:35

标签: php

在课堂上,我在第22行得到了一个“意外的”[...],我可以为我的生活找不到原因。

错误在第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语句。两者都没有效果。

2 个答案:

答案 0 :(得分:5)

这是因为类常量需要具有常量值,并且您的定义不是常量。

来自the manual类常量:

  

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

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

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

答案 1 :(得分:-1)

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

  

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

使用返回基本查询的函数很简单,您可以在任何想要使用它的地方调用它。

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`)";
}