的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 :(得分: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');
您现在应该可以从index.php调用DB::getInstance();
。如果没有正确加载类,您将在php错误日志中看到错误。