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);
}
}
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 ofPDO
.– Woss
I already did that friend didn’t work out
– Adriano Back
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?
– Woss
Updating the question implies editing it. If you changed the error, put the code and the error message in the question.
– Woss
up-to-date ready
– Adriano Back
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.– Woss
worked thanks.
– Adriano Back