-1

I create a singleton class to return my database connection. The only thing I'm doing is calling it's getInstance() function from another php file.

Here is the singleton file:

<?php

namespace stm_asc;

class PDO_Conn
{
    public static $instance;

    private static $host = '127.0.0.1';
    private static $db   = 'stm_asc';
    private static $charset = 'utf8mb4';
    private static $user = 'alekrabbe';
    private static $pass = '<password>';

    private static $dsn;
    private static $opt = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];

    private function __construct() {
        //
    }

    public static function getInstance() {
        if (!isset(self::$instance)) {
            try {
                self::$dsn = "mysql:host=".self::$host.";dbname=".self::$db.";charset=".self::$charset.";";
                self::$instance = new PDO(self::$dsn, self::$user, self::$pass, self::$opt);
                echo "Connected successfully";
            } catch(PDOException $e) {
                echo "Connection failed: " . $e->getMessage();
            }
        }

        return self::$instance;
    }
}

Then what I do is from a login.php call the function getInstance(), like so:

<?php
error_reporting(E_ALL);
ini_set("display_errors","On");
$pdo = \stm_asc\PDO_Conn::getInstance();
//session_start();    
?>

<!DOCTYPE html>
<html>
 [HTML CODE HERE]
</html>

But when I try to loaf login.php I get the following error:

Fatal error: Uncaught Error: Class 'stm_asc\PDO_Conn' not found in /home/alekrabbe/PhpstormProjects/selectorHat/views/login.php:4 Stack trace: #0 {main} thrown in /home/alekrabbe/PhpstormProjects/selectorHat/views/login.php on line 4

I'm not sure why is it not finding the class PDO_Conn, here is my directory structure:

enter image description here

If I comment the line $pdo = \stm_asc\PDO_Conn::getInstance(); everything loads fine.

I'm using PHP 7.2.7 and Ubuntu 18.04


EDIT 1

So I tried using autoloader by following this documentation. However it still not working, here is the code:

<?php
session_start();
spl_autoload_register(function ($name) {
    echo "Want to load $name.\n";
    throw new Exception("Unable to load $name.");
});

try {
    $pdo = \stm_asc\PDO_Conn::getInstance();
    $mapper = new \stm_asc\MySQL_DataMapper($pdo);
    $stuff = $mapper->fetchUserByFname('Alexandre');
    var_dump($stuff);
} catch (Exception $e) {
    echo $e->getMessage(), "\n";
}   


?>

<!DOCTYPE html>
<html>
 [HTML CODE HERE]
</html>

This is the message that appears on the screen:

Want to load stm_asc\PDO_Conn. Unable to load stm_asc\PDO_Conn.

However now the html, css and java script are loading, I believe this is due to the try catch block.

I also tried just adding the require_once '../controller/database/PDO_Conn.php'; at the begining of login.php, so it looks like this:

<?php
require_once '../controller/database/PDO_Conn.php';
$pdo = \stm_asc\PDO_Conn::getInstance();
?>

<!DOCTYPE html>
<html>
 [HTML CODE HERE]
</html>

But the error persists:

Fatal error: Uncaught Error: Class 'stm_asc\PDO_Conn' not found in /home/alekrabbe/PhpstormProjects/selectorHat/views/login.php:22 Stack trace: #0 {main} thrown in /home/alekrabbe/PhpstormProjects/selectorHat/views/login.php on line 22
Alexandre Krabbe
  • 727
  • 1
  • 13
  • 33

1 Answers1

3

Solution 1: Include the class in your login.php.

<?php 
require_once '../path/to/file.php';

In your case: (from login.php)

<?php 
require_once '../controller/database/PDO_Conn.php';

Solution 2: (Recommended)

Use PHP Autoloader: see this simple autoload example I set for you in GitHub.

Additional Resource: how-do-i-use-php-namespaces-with-autoload

tika
  • 7,135
  • 3
  • 51
  • 82
  • The path appears to be `../database/PDO_Conn.php` according to the file hierarchy in the question. – Barmar Jul 09 '18 at 19:52
  • @Barmar That's correct. I just expected the OP to edit the `path/to` as per his/her/it's requirement. – tika Jul 09 '18 at 19:55
  • 1
    You should lower your expectations when answering here. I can't count the number of times someone has tried to use my code literally because they couldn't tell that my value was a placeholder. If the value is known, I try to use it. – Barmar Jul 09 '18 at 20:01
  • Thank you @tika, but it still not working, I edited the question to post my attempts. – Alexandre Krabbe Jul 09 '18 at 20:24
  • @AlexandreKrabbe Are you using a framework? If so, most PHP frameworks come with an autoloader. In that case, you need to use proper namespace (with respect to folder name) Also see the example I set in GitHub. – tika Jul 09 '18 at 21:18