2013-11-22 112 views
-4

我檢查了所有其他相同題目的問題,但我無法解決。我試圖學習PDO的東西:D我認爲只有一個真正的PHP大師可以解決這個問題。並可以爲學習pdo建議一個網頁。致命錯誤:調用未定義的方法DB :: getInstance()

的index.php

require_once 'core.php'; 
DB::getInstance(); 

core.php中

session_start(); 

$GLOBALS['yapilandirma']= array(
    'mysql' => array(
     'host' => '127.0.0.1', 
     'kullanici_adi' => 'root', 
     'sifre' => '', 
     'db' => 'mmogezgini' 
    ), 
    'hatirla' => array(
     'cerez_adi' => 'hash', 
     'cerez_bitis_suresi' => 604800 
    ), 
    'oturum' => array(
     'oturum_adi' => 'kullanici' 
    ) 
); 

spl_autoload_register(function($class){ 
    require_once $class . '.php'; 
}); 

require_once 'Sterilize.php'; 

$con = mysqli_connect("localhost","root","","mmogezgini"); 
header('Content-Type: text/html; charset=utf-8'); 
mysqli_set_charset($con, "utf8") 

Yapilandirma.php

class Yapilandirma{ 
    public static function get($yol = null){ 
     if($yol){ 
      $yapilandirma = $GLOBALS['yapilandirma']; 
      $yol = explode('/',$yol); 
      print_r($yol); 

      foreach($yol as $bit){ 
       if(isset($yapilandirma[$bit])) { 
        $yapilandirma = $yapilandirma[$bit]; 
       } 
      } 

      return $yapilandirma; 
     } 

     return false; 
    } 
} 

db.php中

class DB { 
    private static $_instance = null; 
    private $_pdo, 
    $_query, 
    $_hata = false, 
    $_sonuclar, 
    $_count = 0; 

    private function __construct() { 
     try { 
      $this->_pdo = new PDO('mysql:host=' . Yapilandirma::get('mysql/host') .';dbname=' . Yapilandirma::get('mysql/db'), Yapilandirma::get('mysql/kullanici_adi'), Yapilandirma::get('mysql/sifre')); 
     } catch(PDOException $e){ 
      die($e->getMessage()); 
     } 
    } 
     public static function getInstance(){ 
     if(!isset(self::$_instance)){ 
      self::$_instance = new DB(); 
      } 
      return self::$_instance; 
     } 


} 
+0

這些文件都在同一個目錄中嗎?在同一目錄 – Ing

+0

是的,但我現在看到我使用需要在另一個頁面的話,我應該寫文件目錄爲 – user3018898

回答

0

如果所有文件都在同一文件夾,那麼這autolader應該工作:

core.php中:

function autoloader($className) { 
    $filename = realpath(__DIR__) . '/' . $className . '.php'; 

    if (file_exists($filename)) { 
     require_once($filename); 
    } else { 
     trigger_error("$filename not found"); 
    } 

    return true; 
} 

spl_autoload_register(__NAMESPACE__ . '\autoloader'); 

您現在應該能夠調用DB::getInstance();從index.php文件。如果類沒有正確加載,你會在你的php錯誤日誌中看到一個錯誤。

+0

問題解決了這是錯誤的文件名/路徑 這是 spl_autoload_register(函數($類){ \t require_once $類'.php'; \t }); 我改變到並解決的xD spl_autoload_register(函數($類){ \t require_once 'TEMA/parca /' $類 '.PHP'; \t }); 我討厭誰給直接 - 否定點爲什麼你們這樣做,我試圖學習一些東西,我會幫助別人,當我學習。 – user3018898

+0

@ user3018898好消息!儘量不要像'tema/parca /'那樣硬編碼你的路徑。例如,您可以使用'realpath(__ DIR __)'來返回當前目錄的實際路徑。或者你可以在index.php中定義一個帶有應用程序根路徑的全局常量,然後使用相對路徑。如果它有用,請接受答案。 –

+0

@ user3018898我認爲向下投票更多的是格式化,而不是問題本身...... –

相關問題