Why can\'t I do this in PHP? Where Database
is a singleton class and getInstance() returns a PDO object.
http://php.net/language.oop5.properties
Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They 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.
The important part is
that is, it must be able to be evaluated at compile time
Expressions were evaluated at runtime, so it isn't possible to use expression to initialize properties: They are simply not evaluatable yet.
RTM ;)
See the last sentence of the first paragraph in the PHP documentation for properties http://www.php.net/manual/en/language.oop5.properties.php
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.
You can't execute code to produce the value of a static variable as, by definition, static variables are affected at compile time, see :
Getting the run-time value of a variable, or calling a function (run-time too) can't be done at compile-time to they cannot be affected to static variables.