PDO class not found in namespace other than global

Asked

Viewed 216 times

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.

PDO information in phpinfo(): inserir a descrição da imagem aqui

  • I don’t know if it interferes with anything, but I’ve already taken the backslash, just doing use PDO;?

  • I have tried tbm. Before posting my question, I saw some posts about, but did not interfere with the result

  • Exactly the same error message? PHP searching for the PDO class in the namespace Classes?

  • Exactly.... :/

  • 1

    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.

  • Even using the bar on new \PDO() php cannot find the class in the global namespace

  • @Cesarandré, in this case, is probably missing some extension. What version of your php? And the operating system?

  • @jlHertel PHP 5.6.14 and Windows 7

  • @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 http://imgur.com/a/tnMdm

  • @Cesarandré, I do not have access to Imgur. Could update your post with the image, or just answer the questions?

  • @Cesarandré, could post the error you received when using the syntax new \PDO() ?

  • is the same mistake I put in the question

  • 2

    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.

  • What’s more, your error appears Classes\PDO and the namespace of the file in question is Classes\Config. Your problem is elsewhere.

  • @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.

Show 11 more comments
No answers

Browser other questions tagged

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