If you are using PHP >= 5.3, you could use HEREDOC syntax to declare your string :
class MyClass {
public $str = <<<STR
this is
a long
string
STR;
}
$a = new MyClass();
var_dump($a->str);
But this :
- is only possible with PHP >= 5.3
- and the string must not contain any variable
- this is because the string's value must be known at compile-time
- which, btw, explains why the concatenation, with the
.
, will not work : it's done at execution time.
And another drawback is that this will put newlines in the string -- which might, or not, be a bad thing.
If you are using PHP <= 5.2 :
You can't do that ; a solution could be to initialize the string in your class' constructor :
class MyClass {
public $str;
public function __construct() {
$this->str = <<<STR
this is
a long
string
STR;
}
}
(same not with newlines)
Or, here, you can do strings concatenations :
class MyClass {
public $str;
public function __construct() {
$this->str = 'this is' .
'a long' .
'string';
}
}
(this way, no newlines)
Else, you can have a string that's surrounded by either single or double quotes, and put it on several lines :
class MyClass {
public $str = "this is
a long
string";
}
(Here, again, you'll have newlines in the resulting string)