0

I discover the PDO and I develop a blog project by following a tutorial. I am now connecting to the database after creating my Autoloader. From the moment I created my query to retrieve the results, I have the following error:


Notice: Undefined variable: class_name in /Applications/MAMP/htdocs/php/Bilal/app/Autoloader.php on line 18

Warning: require(/Applications/MAMP/htdocs/php/Bilal/app/.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/php/Bilal/app/Autoloader.php on line 18

Fatal error: require(): Failed opening required '/Applications/MAMP/htdocs/php/Bilal/app/.php' (include_path='.:/Applications/MAMP/bin/php/php7.2.1/lib/php') in /Applications/MAMP/htdocs/php/Bilal/app/Autoloader.php on line 18


Here is my file (Autoloader.php) :


<?php
namespace App;

/**
 * Class Autoloader
 */
class Autoloader{

static function register(){
    spl_autoload_register(array(__CLASS__, 'autoload'));
}

    static function autoload($class){
        if (strpos($class, __NAMESPACE__ . '\\') === 0){
            $class = str_replace(__NAMESPACE__ . '\\', '', $class);
            $class = str_replace('\\', '/', $class);
            require __DIR__ . '/'. $class_name . '.php';
        }
    }
}

And this is my file (Database.php)


<?php
namespace App;

use \PDO;

class Database{
    private $db_name;
    private $db_user;
    private $db_pass;
    private $db_host;
    private $pdo;

    public function __construct($db_name, $db_user = 'root', $db_pass  = 'root', $db_host = 'localhost'){
        $this->db_name = $db_name;
        $this->db_user = $db_user;
        $this->db_pass = $db_pass;
        $this->db_host = $db_host;
    }

    /**
     * Accesser for connection to database
     */
    private function getPDO(){
        if($this->pdo === nul){
            $pdo = new PDO('mysql:host='.$this->db_host.';dbname='.$this->db_name, $this->db_user, $this->db_pass);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->pdo = $pdo;
        }
        return $this->pdo;
    }

    public function query($statement){
        $req = $this->getPDO()->query($statement);;
        // On exécute la requète avec fetchAll
        $datas = $req>fetchAll(PDO::FETCH_OBJ);
        return $datas;
    }
}

I cannot find the issue and asking for help, please.

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Nuno
  • 1
  • 2
  • Start with __understanding__ that `$class_name` is __not defined anywhere__. – u_mulder Jun 02 '18 at 18:13
  • Thanks for your answer. I just realized that I named my variable differently : $class_name instead of $class... – Nuno Jun 02 '18 at 18:52

0 Answers0