problem with namespace and PDO

Asked

Viewed 226 times

2

Well I got a class Sql extending PDO, but when I define a namespace the following error message appears when creating an object from the Sql class:

Fatal error: Class 'PDO-related classes' not found in var/www/html/DAO/classes/connected/Sql.php on line 3

it’s as if the PDO was being called from inside the namespace I created, can anyone help me? I’m new to PHP

class below:

<?php
    namespace classes\conexao;
        class Sql extends PDO{
            private $conn;

            public function __construct(){
                $this->conn = new PDO("mysql:host=localhost; dbname=dbphp7", "root", "123456");
            }

            private function setParams($statment, $parameters = array()){
                foreach($parameters as $key => $value){
                    $this->setParam($statement, $key, $value);
                }
            }

            private function setParam($statment, $key, $value){
                $statment->bindParam($key, $value);
            }

            public function query($rawQuery, $params = array()){
                $stmt = $this->conn->prepare($rawQuery);

                $this->setParams($stmt, $params);

                return $stmt->execute();
            }

            public function select($rawQuery, $params = array()):array{
                $stmt = $this->query($rawQuery, $params);

                return $stmt->fetchAll(PDO::FETCH_ASSOC);                
            }

            public function getConn() {
                return $this->conn;
            }

            public function setConn($conn) {
                $this->conn = $conn;
            }


        }
        ?>
  • 1

    Change to extends PDO

  • Thanks a lot, man, I hope I can contribute soon with the guys, thanks a lot

1 answer

0

He’s interpreting the PDO as any class of his system, what you can do is:

You can change:

class Sql extends PDO{

To

class Sql extends \PDO{

Or import using the use command at the top of the document, below the namespace:

namespace classes\conexao;
use PDO

To continue this way:

class Sql extends PDO{

Browser other questions tagged

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