PDO como estender a conexao para outros arquivos
Iai galera beleza? Estou com um erro simples aqui como eu faço para estender minha conexão para outros arquivos ? Estou tentado fazer mas está dado um erro aqui
arquivo connection.php
<?php
class Conexao{
public $db_name = 'pessoa';
public $host = 'localhost';
public $user = 'root';
public $pass = '';
public function __construct()
{
try {
$pdo = new PDO("mysql:db_name=".$this->db_name.";host=".$this->host,$this->user,$this->pass);
} catch (PDOException $e) {
echo "Erro no banco ".$e->getMessage();
exit();
}
}
}
arquivo** Pessoa.php**
<?php
include 'connection.php';
class Pessoa {
private $pdo;
public function __construct()
{
$this->pdo = new Conexao();
}
public function buscarDados(){
$stmt = [];
$sql = $this->pdo->query("SELECT * FROM pessoa
order by id desc");
$stmt = $sql->fetchAll(PDO::FETCH_ASSOC);
return $stmt;
}
}
arquivo **index.php**
<?php
require_once 'models/Pessoa.php';
$p = new Pessoa();
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="assets/css/style.css">
<title>Crud PDO </title>
</head>
<body>
<section id="esquerda">
<form action="">
<h2>Cadastrar Pessoa</h2>
<label for="nome">Nome: </label>
<input type="text" name="nome" id="nome">
<label for="telefone">Telefone: </label>
<input type="text" name="telefone" id="telefone">
<label for="email">Email:</label>
<input type="text" name="email" id="email">
<input type="submit" value="Cadastrar">
</form>
</section>
<section id="direita">
<?php
$p->buscarDados();
?>
<table>
<tr id="titulo">
<td>Nome</td>
<td>Telefone</td>
<td colspan="2">Email</td>
</tr>
<tr>
<td>Maria</td>
<td>00000000</td>
<td>maria@gmail.com</td>
<td><a href="">Editar</a> <a href="">Excluir</a></td>
</tr>
</table>
</section>
</body>
</html>
Erro que é apresentado:
**Fatal error**: Uncaught Error: Call to undefined method Conexao::query()Discussão (2)
Carregando comentários...