Problems with PDO class not found

Asked

Viewed 550 times

0

I am studying a book in which I have to make some queryes through a Pattern Tabledatagateway design but I am getting the following error message:

Fatal error: Uncaught Error: Class 'classes tgd PDO' not found in C: xampp htdocs book_php_poo_12_persistencia classes tgd Connect.php:12 Stack trace:

0 C: xampp htdocs livro_php_poo_12_persistencia for example_table_data_gateway2.php(24): classes tgd Connect->__Construct()

1 {main} thrown in C: xampp htdocs libro_php_poo_12_persistencia classes tgd Connect.php on line 12

index

product

    <?php
namespace classes\tgd;
class Produto {
    private static $conn;
    private $data;

    public static function setConnection(PDO $conn){

        self::$conn = $conn; 
        ProdutoGateway::setConnection(self::$conn);

    }
    function __get($prop) {
        return $this->data[$prop];
    }
//...
}

Tabledatagateway

namespace classes\tgd;

class ProdutoGateway {

    private static $conn;

    public function setConnection(PDO $conn){
        self::$conn = $conn;
    }
    //fazendo buscar por id
    public function find($id,$class = 'stdClass'){
        $sql = "SELECT * FROM produto WHERE id=:id";
        print "$sql<br>\n";
        $result = self::$conn->prepare($sql);
        $result->bindValue(":id",$id,PDO::PARAM_INT);
        $result->execute();
        return $result->fetchObject($class);
    }

    //...
}

connection

<?php
namespace classes\tgd;


class Connect {
    private static $conn;
    const CONN = array('HOST'=>'xxx','USER'=>'xxx','PASS'=>xxx,'DATA'=>'xxx');

    public function __construct(){
        self::$conn = new \PDO("mysql:host=".self::CONN['HOST'].";"
        . "dbname=".self::CONN['DATA'],self::CONN['USER'],self::CONN['PASS']);
        $conn = self::$conn;
        $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        return Produto::setConnection($conn);
    }
}

  • 2

    You are utilizing a global class within a namespace, then you need a backslash. In the methods that receive the PDO as parameter, put \PDO instead of PDO.

  • I already did that friend didn’t work out

  • 1

    From the cited error message, I faithfully believe that this was the error. Is there any way to update the question with the new code? In fact, the error message remained the same?

  • 1

    Updating the question implies editing it. If you changed the error, put the code and the error message in the question.

  • up-to-date ready

  • 2

    Adriano, the problem was in the method parameter, not in the instance. To simplify, remove this backslash that you put and put use PDO in all files referring to the class, including Tabledatagateway and product.

  • worked thanks.

Show 2 more comments

1 answer

4

When you reference a class within a namespace without identifying it with the instruction use, PHP will understand that this class will also belong to namespace. I mean, in doing:

<?php

namespace classes\tgd;

class Foo {
    public function method(PDO $pdo) {}
}

PHP will understand that the class PDO belongs to the namespace classes\tgd. To solve this, whenever you use the class within a namespace, you need to identify its origin. As the PDO belongs to the global context, just put:

use PDO;

Getting something like:

<?php

namespace classes\tgd;

use PDO;

class Foo {
    public function method(PDO $pdo) {}
}
  • Perfect, is that I just entered this world of namespaces e design patterns

Browser other questions tagged

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