Sırayla o sınıfın yöntemini çağıran bir işleve bir tek sınıftan Çağrı

1 Cevap php

Ben hala düzgün (Ben bir yerli konuşmacı değilim ...) ifade için bir yol arıyorum.

[(0)] Yani (bilinen nedenlerle) singleton deseni uygulayan SQL bu sınıf var ve ben de {birini kullanarak veritabanını sorgular bu işlevi, checkUsr(), var } 's yöntemleri.

Herşey sürece ben checkUsr() SQL sınıf içinde deme gibi çalışıyor. Ben bunu yaparken, komut sadece çıkar ve boş bir sayfa görüntülenir - hiçbir hata hiçbir istisnası atılır, döndürülen ... Ne oluyor? Ve nasıl bu soruna mı?

EDIT:

Burada bazı kod:

class SQL
{
  public static function singleton()
  {
    static $instance;
    if(!isset($instance))
      $instance = new SQL;
    return $instance;
  }

  public function someOtherFun()
  {
    checkUsr();
  }

  public function tryLoginAuthor( $login, $sha1 )
  {
    // SQL query
  }
}

function checkUsr()
{
    if (!isset($_SESSION['login']) || !isset($_SESSION['sha1']))
        throw new Exception('Not logged in', 1);
    $SQL = SQL::singleton();
    $res = $SQL->tryLoginAuthor($_SESSION['login'], $_SESSION['sha1']);
    if (!isset($res[0]))
      throw new Exception('Not logged in', 1);
}

Ben SQL sınıf içinde checkUsr çağırdığınızda Yani sorun oluşur. Başka bir sınıftan çağrıldığında Ancak, olmaz ...

1 Cevap

You have to turn on error_reporting, to see the error messages by php, otherwise you will get the blank page you describe. At the top of your index.php file, include these:

ini_set('display_errors', true);
error_reporting(E_ALL | E_STRICT);

Üretim makinede kapatmak unutmayın, bu sadece geliştirme için.

Yerine iç sınıfın işlevi içinde statik değişken $ örneği, ilan etti. Bu iki tamamen farklı şeylerdir. Static değişkenler here kullanımını görmek ve bir statik sınıf özelliği kullanımını görmek here. Sen sonuncusunu gerekir, bu yüzden bu kodunuzu değiştirin:

class SQL {
    static $instance;
    public static function singleton()
      {
        if(!isset(self::$instance))
          self::$instance = new SQL;
        return self::$instance;
      }
...

}

Implementing a SQL class, or any kind of database access as a singleton is a very bad idea, it's going to bite you very hard in the long run. If it turns out that you need to support another database, like you need to pull information from a forum, that is in a different DB than your site, you will have serious problems.