1

Possible Duplicate:
declare property as object?

Why is this invalid in php

public $var1 = 'hello ' . 'world';

I know you have to assign the string to the property within a method but I was just wondering why this is the case ?

Community
  • 1
  • 1
Oliver Bayes-Shelton
  • 6,135
  • 11
  • 52
  • 88
  • I was asking for a quick answer not a negative comment which doesn't help and which seems to be an increasingly but issue with this site. – Oliver Bayes-Shelton Jul 06 '12 at 10:50
  • Before writing here you should really have a problem and try to find an answer for yourself. This answer you can find here in many posts or read on php.net die documentation. There are enough examples and webbandet has posted the correct answer. – René Höhle Jul 06 '12 at 10:54
  • @OliverBS: If you take the time to read the linked answer you will find that it answers the question directly (the relevant part is quoted **in bold** no less). Also, if it matters to you, IMHO the comment is not negative at all. Reading the manual will serve you well. – Jon Jul 06 '12 at 10:54
  • That is true however I wanted a simple answer not a load of links, I also wanted to comment on the answer so I got a better underrating of the question and not have to look at an old answer which might not answer my question 100% – Oliver Bayes-Shelton Jul 06 '12 at 10:55
  • @OliverBS: This site is not your personal genie. What you want will have to be balanced against what is best for the community as a whole (which duplicate questions are not). If that answer still leaves you with questions it's perfectly OK to *tell us you have already read it* but still do not understand X or Y well enough. – Jon Jul 06 '12 at 10:57
  • @OliverBS Half of the comment is autogenerated by SO after marking it as duplicate. If you felt offended I'm sorry, but I just wanted to point you into the direction to go. It is named "exact duplicate", because it is. It answers your question 100%. – KingCrunch Jul 06 '12 at 10:58
  • Sorry Jon and KingCrunch if it sounded like I was being a bit of a kid, I totally see your point and I will try to have a read through other posts next time, have a good day :) – Oliver Bayes-Shelton Jul 06 '12 at 14:50

1 Answers1

3

Due to PHP Docs:

Properties are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value - that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

s.webbandit
  • 16,332
  • 16
  • 58
  • 82