0
I’m creating a class that uses PDO, but in a different namespace than global:
namespace Classes\Config;
use \PDO;
class Connection {
public static function getConnection() {
$dbhost = "algumhost";
$dbuser = "algumuser";
$dbpass = "algumasenha";
$dbname = "algumabase";
$dbms = "algumsgbd";
$dbport = "9999";
$dsn = "$dbms:host=$dbhost;port=$dbport;dbname=$dbname;user=$dbuser;password=$dbpass";
try {
$dblink = new PDO ( $dsn );
$dblink->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo 'Erro na conexão: ' . $e->getMessage();
}
return $dblink;
}
}
After trying to use the getConnecton method, I end up getting this error:
Fatal error: Class 'Classes\PDO' not found in path/para/meuarquivo.php on line 69
Does anyone know what I might be doing wrong? Thanks in advance.
P.S.: I use Composer to autoload my project.
I don’t know if it interferes with anything, but I’ve already taken the backslash, just doing
use PDO;
?– Woss
I have tried tbm. Before posting my question, I saw some posts about, but did not interfere with the result
– Cesar André
Exactly the same error message? PHP searching for the PDO class in the namespace
Classes
?– Woss
Exactly.... :/
– Cesar André
Why not use the class directly:
new \PDO()
? As far as I understand it, it is not possible to import the namespace classes directly. But that doesn’t make sense either, since you can just put the first bar in front.– jlHertel
Even using the bar on
new \PDO()
php cannot find the class in the global namespace– Cesar André
@Cesarandré, in this case, is probably missing some extension. What version of your php? And the operating system?
– jlHertel
@jlHertel PHP 5.6.14 and Windows 7
– Cesar André
@Cesarandré, could you check the output of the phpinfo() command. Is there a section talking about the PDO? If so, which drivers were loaded?
– jlHertel
@jlHertel http://imgur.com/a/tnMdm
– Cesar André
@Cesarandré, I do not have access to Imgur. Could update your post with the image, or just answer the questions?
– jlHertel
@Cesarandré, could post the error you received when using the syntax
new \PDO()
?– jlHertel
is the same mistake I put in the question
– Cesar André
It’s not just
new \PDO
. There are also constants\PDO::ATTR_ERRMOD
and\PDO::ERRMODE_EXCEPTION
, and the Exception\PDOException
. Get used to using backslash for everything global.– Gabriel Heming
What’s more, your error appears
Classes\PDO
and the namespace of the file in question isClasses\Config
. Your problem is elsewhere.– Gabriel Heming
@Cesarandre, Gabriel is right. The mistake can’t be the same, because you changed the namespace. Please update the question with the command you tried along with the received error.
– jlHertel