0

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.phpfile 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);
?>
Yamaha32088
  • 4,125
  • 9
  • 46
  • 97
  • 2
    Where do you load the class file? I see the attempt to instantiate it, and the autoload method but nothing that specifically states this autoload is hitting the UserMethods.php file. Also, why the redundancy in the directorys array? – Kai Qing Sep 21 '13 at 20:40
  • I actually just figured it out I was calling the same directory twice I changed the second directory to `Applications/User/UserMethods but now I am getting a few more errors that I think I can resolve – Yamaha32088 Sep 21 '13 at 20:42
  • `Catchable fatal error: Argument 1 passed to DatabaseConnection::__construct() must be an instance of PDO, none given, called in /var/www/EncoreCMS/test.php on line 25 and defined in /var/www/EncoreCMS/Applications/Database/Classes/DatabaseConnection.php on line 9` – Yamaha32088 Sep 21 '13 at 20:44
  • You violate the `LSP` and `SRP` at the same time!!! – Yang Sep 21 '13 at 21:57
  • What do you mean I violate LSP and SRP – Yamaha32088 Sep 21 '13 at 22:24
  • 1
    @Yamaha32088 Google for SOLID principles – Yang Sep 21 '13 at 23:19
  • My guess is your referring to the two methods in my `UserMethods.php` file. The `retrievePictures()` method I put in there to do some testing it was never going to be part of the real code. I am still a total beginner especially in OOP so any help is appreciated. I found a book on the SOLID principles I am reading up on. Is there any other problems you spot with the code that I am doing wrong I would like to learn to do things the right way first instead of relearning to break bad habits. – Yamaha32088 Sep 22 '13 at 00:04
  • First of all, use [`spl_autoload_register()`](http://www.php.net/manual/en/function.spl-autoload-register.php). As for PDO, you might benefit from [this post](http://stackoverflow.com/a/11369679/727208). Oh .. and use absolute paths in the autoloader, or you will have very hard time in keeping track of directories. – tereško Sep 26 '13 at 05:05

1 Answers1

-1

I fixed the problem see updated code

Yamaha32088
  • 4,125
  • 9
  • 46
  • 97