0

我无法让我的代码正常工作,在尝试将其全部分割成单独的类文件之前它运行良好。我收到错误消息Fatal error: Class 'UserMethods' not found in /var/www/EncoreCMS/test.php on line 25。我有一个DatabaseConnections.php文件,其中包含用于建立与数据库的连接的类和方法。还有一个UserMethods.php文件将retrievePassword()方法存储在UserMethods类中。我的代码如下:

数据库连接.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;
        }
    }

?>

用户方法.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;
        }
    }

?>

测试.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);
?>
4

1 回答 1

-1

我解决了问题,请参阅更新的代码

于 2013-09-21T20:54:50.433 回答