2011-04-05 45 views
0

對象的初始值數組。對象的初始值數組

class test{ 
    private $H_headers = array("A","B K ".chr(128),"C","D"); 
               //Why I can not init this value? 
    ... 
    } 
} 
Multiple annotations found at this line: 
    - syntax error, unexpected ',' 
    - syntax error, unexpected '.', 
    expecting ')' 

,但通常我可以:

$H_headers = array("A","B K ".chr(128),"C","D"); 
+2

可能重複[?爲什麼不PHP屬性允許功能(http://stackoverflow.com/questions/3960323/why -dont-php-attributes-allow-functions) – 2011-04-05 14:12:18

+0

是的,我明白了,但不容易理解他們回答的是什麼?但對我來說簡單得多,看看這個我仍然無法解決我的問題。 – kn3l 2011-04-05 14:16:35

+0

簡而言之:你不能在那裏使用功能。只是不使用功能那裏。 – KingCrunch 2011-04-05 14:24:27

回答

3

佩卡已經提供了一個解決方案,但缺點是,該類必須實現一個構造函數只是分配一個值。因爲要調用的函數是不是特殊的(只是得到一個特定的ASCII碼字符),你也可以使用此

class test{ 
    private $H_headers = array("A","B K \x80","C","D");//Why I can not init this value more here 
    } 
} 

80是十六進制128\x告訴PHP,你想這是一個人物。

更新: 東西可以讀一下吧:)

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double

+1

這是一個公平點,可能更適合OP的情況,+1。 – 2011-04-05 14:32:24

+0

是的,我的case.thanks可以接受 – kn3l 2011-04-05 14:45:50

1

這是不可能做你的類定義想要的東西。重複的鏈接討論了爲什麼這樣設計。

最好的解決方法是做在構造函數賦值:

class test { 

    private $H_headers = null; 

    function __construct() 
    { $this->H_headers = array("A","B K ".chr(128),"C","D"); } 

}