在这种情况下是什么导致了语法错误


What is causing the syntax error in this case?

所以我的class开头是

final class MyWordPressSite
{
    const ROOTURL = 'http://mywebsite/mysubsite';
    const THEMEROOTURL = self::$ROOTURL . '/wp-content/themes/allytics_theme';
    function getRootUrl ( ) 
    {
        return self::ROOTURL;
    }

self::$ROOTURL . '/wp-content/themes/mytheme';中的.被标记为

语法错误:意外的令牌'$ROOTURL'

知道为什么吗?在我看来一切都很正常。我安装了一个PHP语法高亮显示,它可能错误地标记了那个点。

引用自PHP文档

该值必须是常量表达式,而不是(例如)变量、属性、数学运算的结果或函数调用。

const THEMEROOTURL = self::$ROOTURL . '/wp-content/themes/allytics_theme';

不遵守这个限制:

self::$ROOTURL不存在,因为这将是一个静态变量引用,甚至不是对先前定义的常量的引用,应该引用self::ROOTURL

和使用连接是一个操作(在PHP 5.6.0之前不支持)

您应该使用。

private static $ROOTURL = 'http://mywebsite/mysubsite';