Well, folks, I'm doing a project here and giving me some problems, I think I'm mixing things up.
I installed the composer to use autoload and other dependencies, but now I have a problem:
The error you give me is as follows:
Fatal error: Uncaught Error: Class 'Base' not found in C:\Apache24\htdocs\cadUsuario.php:8 Stack trace: #0 {main} thrown in C:\Apache24\htdocs\cadUsuario.php on line 8
The files look like this:
composer.json
"require": {
"php": ">=7.1",
"phpmailer/phpmailer": "~5.2"
},
"autoload": {
"psr-4": {"Photobooker\": "class/"}
}
My classes are, Base and Connection:
Base :
<?php
namespace Photobooker;
require_once "vendor/autoload.php";
$db = Conexao::getInstance();
Connection :
namespace Photobooker;
class Conexao
{
private static $pdo;
private function __construct() {}
public static function getInstance() {
$conn = require(__DIR__ . '/../dbconfig.php');
if (!isset(self::$pdo)) {
try {
self::$pdo = new PDO("pgsql:host=$conn[host]; dbname=$conn[dbname]", $conn['user'], $conn['pass']);
self::$pdo = new PDO("pgsql:host=$conn[host]; dbname=$conn[dbname]", $conn['user'], $conn['pass']);
} catch (PDOException $e) {
print "Erro: " . $e->getMessage();
}
}
return self::$pdo;
}
}
The file that calls this all is called cadUsuario.php
and is started like this:
<?php
require "vendor/autoload.php";
$pdo = Base::getInstance($db, "usuario");
The autoload
there should not call the class Base
?
I changed the following:
on a username:
require "vendor/autoload.php";
$pdo = Photobooker\Base::getInstance($db, "usuario");
in Connection:
namespace Photobooker;
class Conexao
{
private static $pdo;
private function __construct()
{
}
public static function getInstance() {
$conn = require(__DIR__ . '/../dbconfig.php');
if (!isset(self::$pdo)) {
try {
self::$pdo = new PDO("pgsql:host=$conn[host]; dbname=$conn[dbname]", $conn['user'], $conn['pass']);
self::$pdo = new PDO("pgsql:host=$conn[host]; dbname=$conn[dbname]", $conn['user'], $conn['pass']);
} catch (PDOException $e) {
print "Erro: " . $e->getMessage();
}
}
return self::$pdo;
}
}
and Base:
include_once "Conexao.php";
$db = Photobooker\Conexao::getInstance();
class Base {
private $pdo = null; // Atributo para guardar uma conexão PDO
private $tabela = null; // Atributo onde será guardado o nome da tabela
private static $crud = null; // Atributo estático que contém uma instância da própria classe
public $nRows = null;
And now the error that returns is:
Fatal error: Uncaught Error: Class 'Photobooker\PDO' not found in C:\Apache24\htdocs\class\Conexao.php:19 Stack trace: #0 C:\Apache24\htdocs\class\Base.php(6): Photobooker\Conexao::getInstance() #1 C:\Apache24\htdocs\vendor\composer\ClassLoader.php(440): include('C:\Apache24\htd...') #2 C:\Apache24\htdocs\vendor\composer\ClassLoader.php(322): Composer\Autoload\includeFile('C:\Apache24\htd...') #3 [internal function]: Composer\Autoload\ClassLoader->loadClass('Photobooker\Bas...') #4 C:\Apache24\htdocs\cadUsuario.php(8): spl_autoload_call('Photobooker\Bas...') #5 {main} thrown in C:\Apache24\htdocs\class\Conexao.php on line 19
If I add in Connection:
use PDO;
The error changes to:
Fatal error: Uncaught Error: Class 'Photobooker\Base' not found in C:\Apache24\htdocs\cadUsuario.php:8 Stack trace: #0 {main} thrown in C:\Apache24\htdocs\cadUsuario.php on line 8