tree em
tree em

Reputation: 21721

init value array for a object

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");

Upvotes: 0

Views: 259

Answers (2)

KingCrunch
KingCrunch

Reputation: 131881

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

Upvotes: 3

Pekka
Pekka

Reputation: 449415

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");  } 

} 

Upvotes: 1

Related Questions