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");
3
  • 2
    possible duplicate of Why don't PHP attributes allow functions? Commented Apr 5, 2011 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. Commented Apr 5, 2011 at 14:16
  • In short: You cannot use functions there. Just dont use functions there. Commented Apr 5, 2011 at 14:24

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is a fair point and probably fits the OP's situation better, +1.
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");  } 

} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.