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.
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
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.
– rray