2013-11-22 73 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我认为向下投票更多的是格式化,而不是问题本身...... –

相关问题