0

init value array for a object.

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 ')'

But normally I can:

$H_headers = array("A","B K ".chr(128),"C","D");
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
tree em
  • 20,379
  • 30
  • 92
  • 130
  • 2
    possible duplicate of [Why don't PHP attributes allow functions?](http://stackoverflow.com/questions/3960323/why-dont-php-attributes-allow-functions) – Pekka Apr 05 '11 at 14:12
  • yes, I see but it not easy to understand what they answer ?But for me is simpler than.and look at this I still can not solve my problems. – tree em Apr 05 '11 at 14:16
  • In short: You cannot use functions there. Just dont use functions there. – KingCrunch Apr 05 '11 at 14:24

2 Answers2

3

Pekka already provided one solution, but the downside is, that the class must implement a constructor just for assigning a value. Because the function you want to call is not that special (just get a character for a specific ascii code) you can also use this

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

80 is 128 in hexadecimal and the \x tells php, that you want this as a character.

Update: Something to read about it :)

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

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
1

It's not possible to do what you want in the class definition. The duplicate link discusses why this was designed this way.

The best workaround is to do the assignment in the constructor:

class test {  

  private $H_headers = null;

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

} 
Pekka
  • 442,112
  • 142
  • 972
  • 1,088