我无法让代码工作,在尝试将其全部细分为单独的类文件之前,它工作正常。我收到错误Fatal error: Class 'UserMethods' not found in /var/www/EncoreCMS/test.php on line 25
。我有一个DatabaseConnections.php
文件,其中包含用于建立与数据库的连接的类和方法。还有一个UserMethods.php
文件,retrievePassword()
类中存储了UserMethods
方法。我的代码如下:
DatabaseConnections.php:
<?php
require '/resources/library/DB.php';
class DatabaseConnection
{
private $conn = null;
public function __construct(PDO $conn)
{
$this->conn = $conn;
}
protected function getConnection()
{
return $this->conn;
}
}
?>
UserMethods.php:
<?php
class UserMethods extends DatabaseConnection
{
public function retrievePassword($userName)
{
$stmt = $this->getConnection()->prepare('SELECT `password` FROM `users` WHERE `userName`= :userName');
$stmt->bindValue(':userName', $userName);
$stmt->execute();
$salt = $stmt->fetchColumn();
return $salt;
}
public function retrievePictures($userName)
{
$stmt = $this->getConnection()->prepare('SELECT `userName` FROM `users` WHERE `userName`= :userName');
$stmt ->bindValue(':userName', $userName);
$stmt->execute();
$user = $stmt->fetchColumn();
return $user;
}
}
?>
test.php的:
<?php
ini_set('display_errors', true);
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require "$root/resources/library/DB.php";
function __autoload($class_name)
{
//class directories
$directorys = array(
'Applications/Database/Classes/',
'Applications/User/Classes/'
);
//for each directory
foreach($directorys as $directory)
{
//see if the file exsists
if(file_exists($directory.$class_name . '.php'))
{
require_once($directory.$class_name . '.php');
//only require the class once, so quit after to save effort (if you got more, then name them something else
return;
}
}
}
$userName = "testuser";
$a = new UserMethods($conn);
echo $a->retrievePassword($userName);
?>
答案 0 :(得分:-1)
我修复了问题,看了更新的代码