Why can't I do this in PHP? Where Database
is a singleton class and getInstance() returns a PDO object.
<?php
class AnExample
{
protected static $db = Database::getInstance();
public static function doSomeQuery()
{
$stmt = static::$db->query("SELECT * FROM blah");
return $stmt->fetch();
}
}
Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.
http://php.net/manual/en/language.oop5.static.php
Why?!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…