【发布时间】:2017-06-27 03:22:26
【问题描述】:
这是我的 config.php
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'xxxx');
define('DB_USER', 'xxxx');
define('DB_PASS', 'xxxx');
?>
它是DB.php
<?php
include 'config.php';
class DB {
public static $pdo;
public static function connection(){
if (!isset(self::$pdo)) {
try {
self::$pdo = new PDO('mysql:host='.DB_HOST.'; dbname ='.DB_NAME,DB_USER, DB_PASS);
}catch(PDOException $e){
echo $e->getMessage();
}
}
return self::$pdo;
}
public static function prepareOwn($sql){
return self::connection()->prepare($sql);
}
}
?>
第三个文件是Student.php
<?php
include 'DB.php';
class Student {
public $table = 'student_info';
public function readAll(){
$sql = "SELECT * FROM $this->table";
$stmt = DB::prepareOwn($sql);
$stmt->execute();
return $stmt->fetchAll();
}
}
?>
但是当我尝试使用 spl_autoload_register() 从 index.php 访问 readAll() 然后我可以看到 致命错误:调用未定义的方法 DB::prepareOwn() >
谁能帮我解决这个问题?
非常感谢。 萨希杜尔
【问题讨论】:
-
你试过添加断点并单步抛出代码吗?好奇当你触发 realAll 时 DB 是什么。
-
不,我没有。你能给我实现断点来调试问题的源代码吗??
标签: php oop pdo error-handling