I am having trouble getting my code to work, it worked fine before trying to segment it all up into separate class files. I am getting the error Fatal error: Class 'UserMethods' not found in /var/www/EncoreCMS/test.php on line 25
. I have a DatabaseConnections.php
file that contains the class and methods for establishing a connection to the database. There is also a UserMethods.php
file that has the retrievePassword()
method stored in the UserMethods
class. My code is below:
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);
?>