我有一個帶有 OOP 的小型 php 專案。我有 3 個代表物件 User.php Transaction.php 和 BankAccount.php 的 Klasse。如果我在 index.php 中,它們可以正常作業,但是一旦我轉到另一個站點,它就會指出:
Fatal error: Uncaught Error: Call to undefined method database::connect() in /app/public/models/Transaction.php on line 83
專案結構就像這個 app/public models Database.php User.php Transaction.php BankAccount.php sites account.php index.php
它在 index.php 中作業,但是當我登錄并重定向到 sites/account.php 時,突然 Database::connect() 未定義。
提前致謝
Github:https ://github.com/OSZII/eBanking_APP
索引.php:
<?php
session_start();
require_once("./sites/validate.php");
require_once("./models/User.php");
require_once("./models/BankAccount.php");
require_once("./models/Transaction.php");
// redirect if logged in
if (isset($_SESSION['username'])) {
if ($_SESSION['role'] == "admin" | $_SESSION['role'] == "worker") {
echo "<script>window.location.href='sites/adminAccount.php'</script>";
// header("Location: ./sites/adminAccount.php");
die();
} else {
echo "<script>window.location.href='sites/account.php'</script>";
// header("Location: ./sites/account.php");
die();
}
}
if(isset($_POST['username']) & isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
$user = User::getByName($username);
$bankaccount = BankAccount::getByUserId($user->getId());
if($user){
if(password_verify($password, $user->getPassword())){
$_SESSION['id'] = $user->getId();
$_SESSION['username'] = $username;
$_SESSION['role'] = $user->getRole();
$_SESSION['IBAN'] = $bankaccount->getIBAN();
echo "<script>window.location.href='index.php'</script>";
die();
}else $errors['password'] = "Password falsch!";
}else $errors['username'] = "Benutzer existiert nicht!";
}
?>
資料庫.php:
<?php
class Database
{
private static $dbName = 'ebanking';
private static $dbHost = 'mysql';
private static $dbUsername = 'root';
private static $dbUserPassword = 'password';
private static $conn = null;
public function __construct()
{
exit('Init function is not allowed');
}
public static function connect()
{
// One connection through whole application
if (null == self::$conn) {
try {
self::$conn = new PDO("mysql:host=" . self::$dbHost . ";" . "dbname=" . self::$dbName, self::$dbUsername, self::$dbUserPassword);
} catch (PDOException $e) {
die($e->getMessage());
}
}
self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return self::$conn;
}
public static function disconnect()
{
self::$conn = null;
}
}
?>
用戶.php:
<?php
require_once("Database.php");
require_once("DatabaseObject.php");
class User implements DatabaseObject{
private $id;
private $username;
private $password;
private $role;
public function save(){
if($this->validate()){
if($this->id != null && $this->id > 0){
$this->update();
} else $this->id = $this->create();
return true;
}
return false;
}
public function create()
{
$db = Database::connect();
$sql = "INSERT INTO users (username, password, role) values (?, ?, ?);";
$stmt = $db->prepare($sql);
$stmt->execute();
Database::disconnect();
}
public function update(){
$db = Database::connect();
$sql = "UPDATE users SET username = ?, password = ?, role = ? WHERE id = ?;";
$stmt = $db->prepare($sql);
$stmt->executer(array($this->username, $this->password, $this->role, $this->id));
Database::disconnect();
}
public static function get($id){
$db = Database::connect();
$sql = "SELECT * FROM users WHERE id = ?;";
$stmt = $db->prepare($sql);
$stmt->execute(array($id));
$user = $stmt->fetchObject("User");
Database::disconnect();
return $user;
}
public static function getByName($username){
$db = Database::connect();
$sql = "SELECT * FROM users WHERE username = ?;";
$stmt = $db->prepare($sql);
$stmt->execute(array($username));
$user = $stmt->fetchObject("User");
Database::disconnect();
return $user;
}
public static function getByIBAN($IBAN){
$db = Database::connect();
$sql = "SELECT * FROM users WHERE IBAN = ?;";
$stmt = $db->prepare($sql);
$stmt->execute(array($IBAN));
$user = $stmt->fetchObject("User");
Database::disconnect();
return $user;
}
public static function getAll()
{
$db = Database::connect();
$sql = "SELECT * FROM users;";
$stmt = $db->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_CLASS, "User");
Database::disconnect();
return $users;
}
public static function delete($id){
$db = Database::connect();
$sql = "DELETE FROM users WHERE id = ?;";
$stmt = $db->prepare($sql);
$stmt->execute(array($id));
Database::disconnect();
}
private function validate(){
return $this->validateHelper($this->username) &
$this->validateHelper($this->password) &
$this->validateHelper($this->role);
}
private function validateHelper($value){
return strlen($value) > 0 ? true : false;
}
//getter & setter cut out to reduce length off this class for stackoverflow
}
帳戶.php:
<?php
session_start();
require_once("../models/Transaction.php");
require_once("../models/BankAccount.php");
require_once("../models/User.php");
// wenn kein username gesetzt ist, dann soll man direkt wieder auf die loginseite zurück geschickt werden
if(!isset($_SESSION['username']) | isset($_GET['logout']) ){
session_destroy();
echo "<script>window.location.href='../index.php'</script>";
// header("Location: ../index.php");
die();
}
echo User::get(1)->getUsername();
if(isset($_POST['transaction'])){
$bankaccount = BankAccount::getByIBAN($_SESSION['IBAN']);
$senderIBAN = $bankaccount->getIBAN();
$senderBIC = $bankaccount->getBIC();
$receiverIBAN = isset($_POST['receiverIBAN'])? $_POST['receiverIBAN'] : "";
$receiverBIC = BankAccount::getByIBAN($receiverIBAN);
$paymentReference = isset($_POST['paymentReference'])? $_POST['paymentReference'] : "";
$purposeOfUse = isset($_POST['purposeOfUse'])? $_POST['purposeOfUse'] : "";
$amount = isset($_POST['betrag'])? $_POST['betrag'] : "";
if($bankaccount->getBalance() >= $amount){
$transaction = new Transaction();
$transaction->setSenderIBAN($senderIBAN);
$transaction->setReceiverIBAN($receiverIBAN);
$transaction->setSenderBIC($senderBIC);
$transaction->setReceiverBIC($receiverBIC);
$transaction->setPaymentReference($paymentReference);
$transaction->setPurposeOfUse($purposeOfUse);
$transaction->setAmount($amount);
$transaction->setDate(date('Y-m-d'));
$transaction->setTime(date('H:i:s'));
$transaction->save();
echo "<script>window.location.href = 'account.php'</script>";
// header("Location: account.php");
die();
}else $transaktionFehlgeschlagen = true;
}
?>
uj5u.com熱心網友回復:
在public/sites/account.phpl。3 你有:require_once("./database.php");
'./' 指的是當前目錄(public/sites),因此該陳述句包含檔案public/sites/database.php。
該檔案定義了一個名為database的類(與app/public/models 中定義的Database類不同)。
這個database類沒有connect()方法,這就是PHP告訴你的原因Call to undefined method database::connect()(請注意小寫d)
此外,為避免此類問題,您可以:
- 重命名您的類以闡明它們的作用(此處的示例:DatabaseConnection / DatabaseQueries,比 Database/database 更容易理解和除錯)
- 使用自動加載器,這將比自己處理 require 陳述句更容易。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/419739.html
標籤:
上一篇:使用創建日期優化日期相關查詢
下一篇:忽略一些資料的平均計算
