phpcomposer-phpautoload

Composer's autoload: Using "require_once vendor/autoload.php" inside an class that it already loaded?


I am using Dotenv\Dotenv library to call $_ENV[] superglobals, which stores the info of the database I'm using on (generic example, but the question repeats on the others contexts).

In the code above, I am NOT importing composer's vendor folder to use $dotenv->load(); Is this a problem?

<?php

namespace Nti\Connect;

use Dotenv\Dotenv;
use PDO;
use PDOException;
use Exception;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Level;

$dotenv = Dotenv::createImmutable(BASE_URL);
$dotenv->load();

class Connect
{
    protected $DB_NAME;
    protected $DB_HOST;
    protected $DB_PASSWD;
    protected $DB_USER;
    protected $DB_TYPE;
    private $pdo;
    private $logger;
    public static $loggerStatic;
    private static $instance = null;
    public function __construct()
    {
        $this->DB_NAME = $_ENV['DB_NAME'];
        $this->DB_HOST = $_ENV['DB_HOST'];
        $this->DB_PASSWD = $_ENV['DB_PASSWD'];
        $this->DB_USER = $_ENV['DB_USER'];
        $this->DB_TYPE = $_ENV['DB_TYPE'];

        if ($_ENV['APP_ENV'] == "development") {
            $this->logger = new Logger('banco_dados');
            $this->logger->pushHandler(new StreamHandler(__DIR__ . '/../../logs/banco_dados.log', Level::Debug));
        }

        try {
            $this->pdo = new PDO("$this->DB_TYPE:dbname=$this->DB_NAME;
            host=$this->DB_HOST", $this->DB_USER, $this->DB_PASSWD);
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            if ($_ENV['APP_ENV'] == "development") {
                $this->logger->info('[DATABASE] Iniciando conexão...');
            }
        } catch (PDOException $e) {
            echo "Erro quanto ao banco de dados: " . $e->getMessage();
            if ($_ENV['APP_ENV'] == "development") {
                $this->logger->error('[DATABASE] Erro na conexão PDO: ' . $this->pdo . $e->getMessage());
            }
        } catch (Exception $e) {
            echo "Erro: " . $e->getMessage();
            if ($_ENV['APP_ENV'] == "development") {
                $this->logger->error('[DATABASE] Erro genérico de PDO: ' . $this->pdo . $e->getMessage());
            }
        }
    }
    public static function getInstance()
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    public function getPdo()
    {
        if ($_ENV['APP_ENV'] == "development") {
            $this->logger->info('[DATABASE] Pegando PDO...');
        }
        return $this->pdo;
    }
}

I used it before and am now removing it. Seaching for it online (and even pure logic), I don't think its necessary. But I am not sure if I should, as I'm starting my journey on PHP.


Solution

  • PHP's autoloading feature basically works like this:

    1. Somewhere in your code, you tell PHP about a function
    2. At any time after that, when PHP encounters the name of a class (or interface, trait, or enum) which doesn't exist, it will call that function, passing it the class name
    3. The function can do whatever it likes, but generally it uses that class name to find a file on disk that contains that class
    4. If the class now exists, the code carries on running; if it still doesn't, you get an Error

    The require_once 'vendor/autoload.php'; is doing step 1 - it's not importing any names, or changing the meaning of anything on the current file, it's running some PHP code that tells PHP "here's a function you should call if you find a class name you don't have a definition for".

    This only needs to happen once per request - when you go to a new URL in the browser, that's a new PHP process, and somewhere early in that process, you want to tell it about the autoloader. But inside that process, you don't have to tell it again in different files - it will carry on using that same function until the end of the request.