I can’t use "use Pdo"

Asked

Viewed 26 times

-2

my project is not working because it gives the following errors: Warning: include(Models PDO.php): Failed to open stream: No such file or directory in C: xampp htdocs Resumeai index.php on line 3
Warning: include(): Failed Opening 'Models PDO.php' for inclusion (include_path='C: xampp php PEAR') in C: xampp htdocs Resumeai index.php on line 3 Fatal error: Uncaught Error: Class "Models PDO" not found in C:

I saw that if you use PDO you can instantiate, but it does not work in the code. It is below.

<?php
   namespace Models;
   use \PDO;
     class database{
        private $connection;
        public function __construct(){
            $this->setConnection();
        }
        public function setConnection(){
            try{
                $this->connection = new PDO("mysql:dbname=resumeai;host=localhost;", "root", "");
                $this->connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                return $this->connection;
            }catch(PDOException $e){
                die('erro ao conectar!');
            }
        }
        public function getPDO(){
            return $this->connection;
        }
    }
?>
 
<?php
    namespace Models;
    require_once 'database.php';
    class HomeModel{
        private $db;
        private $pdo;
         public function __construct(){
             $this->db = new database();
             $this->pdo = $this->db->getPDO();
         }
        public function login($email, $senha){
           
        }
        public function register($nome, $email, $senha){
            $sql = $this->pdo->prepare("INSERT INTO usuario(nome, email, senha) VALUES( ?, ?, ?)");
            $sql->bindValue(1, $nome, PDO::PARAM_STR);
            $sql->bindValue(2, $email, PDO::PARAM_STR);
            $sql->bindValue(3, $senha, PDO::PARAM_STR);
            var_dump($sql);
            $sql->execute();
            // if($sql->execute()){
            //     return true;
            // }else{
            //     return false;
            // }
        }
        public function getC(){
            return $this->pdo;
        }
    }
?>

1 answer

0

You don’t need to put as PDO on use. Use only

use PDO;

You put to only when you will use in the function but not declared in use, for example:

<?php


 // sem declarar no use
class Qualquer {

    public function qualquerCoisa()
    {
    
        ....
        $stmt->bindValue(":PARAM", $valor, \PDO::PARAM_INT);
        ....
    }
}

// ao declarar no use

<?php

use PDO;

class Qualquer {

    public function qualquerCoisa()
    {
    
        ....
        $stmt->bindValue(":PARAM", $valor, PDO::PARAM_INT);
        ....
    }
}

Browser other questions tagged

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