Php 致命错误:调用未定义的方法DB::getInstance()
我检查了所有其他标题相同的问题,但我无法解决。我试图学习PDO:我认为只有真正的php高手才能解决这个问题。并且可以为学习pdo建议一个网页 Index.PHPPhp 致命错误:调用未定义的方法DB::getInstance(),php,pdo,Php,Pdo,我检查了所有其他标题相同的问题,但我无法解决。我试图学习PDO:我认为只有真正的php高手才能解决这个问题。并且可以为学习pdo建议一个网页 Index.PHP require_once 'core.php'; DB::getInstance(); CORE.PHP session_start(); $GLOBALS['yapilandirma']= array( 'mysql' => array( 'host' => '127.0.0.1',
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;
}
}
如果所有文件都在同一文件夹中,则此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错误日志中看到一个错误。这些文件都在同一个目录中吗?是的,在同一个目录中,但我现在看到我在另一个页面中使用了require,然后我应该为该问题编写文件目录解决了文件名/路径错误是spl_autoload_寄存器(函数($class){require_once$class.'.php';});我改为这个,并解决了xD spl_autoload_寄存器(函数($class){require_once'tema/parca/'.$class..php';});我讨厌有人直接否定你们为什么这么做,我试图学习一些东西,我会在学习时帮助别人。@user3018898好消息!尽量不要像tema/parca/
那样硬编码你们的路径。你们可以使用realpath(\uuu DIR\uuuuu)
,例如,返回当前目录的实际路径。或者,您可以在index.php中使用应用程序根路径定义一个全局常量,然后使用相对路径。如果答案有用,请接受它。@user3018898我认为投票更多地与格式有关,而不是与问题本身有关。。。