2013-09-21 106 views
0

我无法让我的代码正常工作,在尝试将它们全部分割为单独的类文件之前,它工作正常。我收到错误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); 
?> 
+2

你在哪里加载类文件?我看到尝试实例化它,并自动加载方法,但没有具体说明这个自动加载是击中UserMethods.php文件。此外,为什么directorys阵列中的冗余? –

+0

我其实只是想通了两次我调用同一个目录两次我将第二个目录更改为'Applications/User/UserMethods,但现在我得到了一些我认为可以解决的错误 – Yamaha32088

+0

'可捕获的致命错误:参数1已通过到DatabaseConnection :: __ construct()必须是PDO的一个实例,没有给出,在第25行调用/var/www/EncoreCMS/test.php并在/ var/www/EncoreCMS/Applications/Database/Classes/DatabaseConnection中定义。 php第9行' – Yamaha32088

回答

-1

我解决了这个问题看更新的代码