对象的初始值数组
问题描述:
对象的初始值数组。对象的初始值数组
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");
答
佩卡已经提供了一个解决方案,但缺点是,该类必须实现一个构造函数只是分配一个值。因为要调用的函数是不是特殊的(只是得到一个特定的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
答
这是不可能做你的类定义想要的东西。重复的链接讨论了为什么这样设计。
最好的解决方法是做在构造函数赋值:
class test {
private $H_headers = null;
function __construct()
{ $this->H_headers = array("A","B K ".chr(128),"C","D"); }
}
可能重复[?为什么不PHP属性允许功能(http://*.com/questions/3960323/why -dont-php-attributes-allow-functions) – 2011-04-05 14:12:18
是的,我明白了,但不容易理解他们回答的是什么?但对我来说简单得多,看看这个我仍然无法解决我的问题。 – kn3l 2011-04-05 14:16:35
简而言之:你不能在那里使用功能。只是不使用功能那里。 – KingCrunch 2011-04-05 14:24:27