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:
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