1
Good people, next, I’m doing a project here and giving me some problems, I think I’m mixing things up.
I installed Composer to use autoload and other dependencies, but now I have a problem:
The mistake you make is this::
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 are like this:
Composer.json
"require": {
"php": ">=7.1",
"phpmailer/phpmailer": "~5.2"
},
"autoload": {
"psr-4": {"Photobooker\\": "class/"}
}
My classes are Base and Connected:
Groundwork:
<?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 it all is called cadUsuario.php
and is thus initiated:
<?php
require "vendor/autoload.php";
$pdo = Base::getInstance($db, "usuario");
The autoload
there should not call the class Base
?
I amended the following:
in cadUsuario:
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 on Basis:
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
Is calling the autoload 2 times?
– Maurivan
But there’s no class
Base
. The methodgetInstance
is stated inConexao
. Base is in fact a class? And don’t forget the namespaces, if your filecadUsuario
do not belong to the samenamespace
, classes shall be instantiated withPhotobooker\Base
.– Woss
@Maurivan Autoload within the Base class, because it uses a method from another class, it is better to import this class directly?
– Hermus
@Andersoncarloswoss the Base class exists yes, it is that I did not put it all there, I put soh the "header". cadUsuario in this case is the controller for users registration. But I will make the changes here.
– Hermus
The autoload is there precisely to prevent the import of classes at all times. Watch the link video for better understanding and use of autoload and namespace. https://youtu.be/UeCVTtRmGE
– Maurivan
@Maurivan great, clarified me enough. Thanks for the help!
– Hermus