2012-07-27 71 views
1

我不明白爲什麼這個錯誤在PHP 5.4中。嚴格標準:非靜態方法dbInstance :: getInstance()

嚴格的標準:非靜態方法dbinstance具備::的getInstance()不應該被靜態調用

類是:

class dbInstance 
{ 
    private static $db; 

    public static function getInstance() 
    { 
     if (! self::$db) self::$db = new db(); 
     return self::$db; 
    } 
} 

我把它想:

$registry->db = $db = dbInstance::getInstance() 

謝謝

+0

你在哪裏實際調用方法有關係嗎? – 2012-07-27 12:44:11

+1

只爲記錄,[單身是邪惡](http://programmers.stackexchange.com/questions/40373/so-singletons-are-bad-then-what) – 2012-07-27 12:44:20

+0

@TimCooper - $ registry-> db = $ db = dbInstance :: getInstance() – keepwalking 2012-07-27 12:46:35

回答

1

我不能複製發生錯誤。你確定你已經編輯了正確的文件嗎?或者,也許你正在看到輸出的緩存版本?

<?php 
$db = dbInstance::getInstance(); 

class dbInstance 
{ 
    private static $db; 

    public static function getInstance() 
    { 
     if (! self::$db) self::$db = new db(); 
     return self::$db; 
    } 
} 

class db { 
    public function __construct() { 
     echo 'db::construct filemtime=', date('Y-m-d H:i:s', filemtime(__FILE__)), ' PHPVERSION=', PHP_VERSION; 
    } 
} 

我的電腦上打印

db::construct filemtime=2012-07-27 14:50:37 PHPVERSION=5.4.1 
相關問題