php5扩展main类并使用static

php5扩展main类并使用static,php,class,static,Php,Class,Static,为什么我不能这样做 <?php class core { public static $db; function __construct() { $this->db = new mysql('host', 'user', 'pw', 'db'); } } class stat extends core { public static function log() { core::$db->query("in

为什么我不能这样做

<?php 
class core {
    public static $db;


    function __construct() {
        $this->db = new mysql('host', 'user', 'pw', 'db');
    }
}

class stat extends core {
    public static function log() {
        core::$db->query("insert into mytable values(now())");
    }
}

// do something
stat::log(); 
?>

根据代码的外观,因为您没有将任何内容分配到$db中。只有在创建类的实例时才调用构造函数,而不是使用静态

还有,为什么您的代码甚至要扩展核心?您不需要扩展它来使用静态方法/变量。也许更合理的做法是实际将其作为实例属性,并使用新实例而不是静态实例

core::\uu construct()方法仅在调用创建对象的
newcore
newstat
时调用。直接转到
stat::log(),
so core::$db从未初始化过