2015-07-10 53 views
-1

所以我開始類出像這種情況下導致語法錯誤的原因是什麼?

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';.被標記爲

Syntax error: Unexpected token '$ROOTURL'

任何想法,爲什麼?一切看起來都適合我。我安裝了一個PHP語法突出顯示器,可能會錯誤地標記該點。

回答

1

PHP Docs

The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.

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

報價不遵守這一限制:

self::$ROOTURL不存在的,因爲這將是一個靜態變量的引用,甚至沒有引用您的以前定義的常數,應該參考代替爲self::ROOTURL

並且使用串聯是o (在PHP 5.6.0之前不支持)

0

您應該使用。

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

然後,它可以在類 –

+0

'私有靜態之外進行修改...'然後:P – CD001

+0

用戶'private',而不是'public'。 請參考這裏的知名度。 http://php.net/manual/en/language.oop5.visibility.php –

相關問題