PHP ve tekrar singletons

3 Cevap php
class ConfigReader
{
    private static $instance = NULL;
    protected $configData = array();                                                                        

    public function getInstance()
    {
        if( self::$instance == NULL )
        {
            self::$instance == new ConfigReader();
        }
        return self::$instance;
    }

    public function getConfigValue( $getName )
    {
        echo 'In function';
    }

    private function __construct()
    {
        $this->configData = %READ_FROM_DATABASE%;
    }

    private function __clone() {}
}

Ve için:

var_dump( ConfigReader::getInstance() )

I got: NULL

Ben, bana yardım ... beyinleri lütfen kırdı.

3 Cevap

Yöntem getInstance olarak, yalnızca bir tane '=' kullanmanız gerekir: Eğer bir atama yapmak istiyorum, ve bir compatison:

self::$instance = new ConfigReader();

Yerine

self::$instance == new ConfigReader();


And that method should be declared as static, as you are using it as a static method :

public static function getInstance()
{
    if( self::$instance == NULL )
    {
        self::$instance = new ConfigReader();
    }
    return self::$instance;
}

Bu iki değişiklik Weth, bu çalışması gerekir ;-)

Sadece bir yazım hatası: self::$instance == new ConfigReader() yerine = ve == içerir

Yöntem getInstance () da statik olmalıdır.