PDO - Class Not Found

Asked

Viewed 253 times

1

What’s going on

I created a static method to return the connection so I can use it on DAO, but in doing so it gives Class Not Found PDO. inserir a descrição da imagem aqui

Other Projects

The first thing a lot of people will think is, "The extension is not activated". Gentlemen, the extensions are properly activated and have already been used in other projects. I have here 2 other projects with PDO, but this time I tried to use Object Orientation in the best way possible, to make the code cleaner.

OBS: If anyone has any suggestions to make some class more 'beautiful', I will be open to modifications.

PDO Ativo

inserir a descrição da imagem aqui

My Code

Class Entidadedao

<?php

namespace Presto\model;
use Presto\model\ConnectionFactory as ConnectionFactory ;

class EntidadeDAO {
    private $connection = null;

    public function __construct() {
        self::$connection = ConnectionFactory::getConnection();
    }
} 

Class Connectionfactory

<?php
namespace Presto\model;

class ConnectionFactory {

public static function getConnection() {
    $connection = null;
    $config = self::configureConnection();

    try {
        self::$conection = new PDO($config['databaseType'].':host='.$config['hostname'].';dbname='.$config['database'],$config['username'],$config['password']);
    }catch (PDOException $e) {
        echo $e->getMessage();
    }

    return self::$connection;
}

public static function configureConnection() {
    $config = array();

    $config['databaseType'] = 'pgsql';
    $config['hostname'] = '127.0.0.1';
    $config['database'] = 'minhaDatabase';
    $config['username'] = 'root';
    $config['password'] = 'password';

    return $config;
}
} 
  • Put the phpinfo print with the PDO part, only p make it clear that the extension has been installed and eliminate this possibility.

1 answer

3


Know how to interpret the error \Presto model PDO not found, when working with namespace, each backslash is a correct directory?

To use the PDO lib or native libraries you need to instantiate, use the backslash first, as it points to the root, i.e., the native PHP libraries.

Example: \PDO

public static function getConnection() {
    $connection = null;
    $config = self::configureConnection();

    try {
        self::$conection = new \PDO($config['databaseType'].':host='.$config['hostname'].';dbname='.$config['database'],$config['username'],$config['password']);
    }catch (\PDOException $e) {
        echo $e->getMessage();
    }

    return self::$connection;
}
  • It is showing Fatal error: Access to undeclared Static Property: Presto model Connectionfactory::$Connection in C: wamp www Testephp app Presto model Connectionfactory.php on line 16. Access to an undeclared static property. I put Static before $Connection and the error remained. I would know what the resolution is ?

  • @Allanramos does not make sense to use Singleton with database, a connection should not be extended, it should only be used, its Connectionfactory is limited to a single connection. To fix this, input the data access, and perpetuate the instance in the correct way through your application. Whether by dependency injection or Registry. But if you want to use it anyway, take a look here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.