2011-12-05 14 views
1

可能性の重複:
Initializing PHP class property declarations with simple expressions yields syntax errorカスタムクラスのインスタンスを保持する連想配列を作成できますか?

それは連想配列を作成し、値としてカスタムクラスのインスタンスを解析することは可能ですか?

ような何か:私は "予想外のキーワードNEW" のようなエラーを取得しています、これをしようと

private static $contentTypeList = array(
    "News" => new ContentType("News", "news.php", "News"), 
    "Termine" => new ContentType("Termine", "termine.php", "Termine"), 
    "Zeitplan" => new ContentType("Zeitplan", "zeitplan.php", "Zeitplan"), 
    "Fotos" => new ContentType("Fotos", "fotos.php", "Fotos"), 
    "Anfahrt" => new ContentType("Anfahrt", "anfahrt.php", "Anfahrt"), 
    "Guestbook" => new ContentType("Guestbook", "guestbook.php", "Gästebuch") 
    ); 

誤った構文を使用していますか、それともできませんか?

答えて

2

これは可能ですが、関数本体の外部でプロパティを宣言するときはできません。

には、例えば、任意の関数にそれを追加します。

それはだと
class SomeClass { 
    private static $contentTypeList = array(); 
    public static function init() { 
     self::$contentTypeList = array(
     "News" => new ContentType("News", "news.php", "News"), 
     "Termine" => new ContentType("Termine", "termine.php", "Termine"), 
     "Zeitplan" => new ContentType("Zeitplan", "zeitplan.php", "Zeitplan"), 
     "Fotos" => new ContentType("Fotos", "fotos.php", "Fotos"), 
     "Anfahrt" => new ContentType("Anfahrt", "anfahrt.php", "Anfahrt"), 
     "Guestbook" => new ContentType("Guestbook", "guestbook.php", "Gästebuch") 
    ); 
    } 
} 
+0

'private'は関連していないようです。実際、あなたのソリューションは最適ではありません。 'init'は' static'ではなく、二相の初期化が必要です。 –

+0

'variable'はやや誤解を招く:それはプロパティに関するものです。 – KingCrunch

+0

@ TomalakGeret'kal:今すぐ修正する必要があります –