The connection to the database is working perfectly, but the getList method returns this error:
Fatal error: Uncaught Error: Call to a member function prepare() on null in /opt/lampp/htdocs/site/config/config.class.php:32 Stack trace: #0 /opt/lampp/htdocs/site/config/config.class.php(43): Conexao->getList() #1 {main} thrown in /opt/lampp/htdocs/site/config/config.class.php on line 32
I understood that this is on line 32, but I can not find anything wrong with this line, can someone please give me a hand? the code is below:
<?php
class Conexao {
private $sql,$user,$pass,$lista;
public function __construct($sql = 'mysql:host=localhost;dbname=teste',$user = 'root', $pass = '') {
$this->sql = $sql;
$this->user = $user;
$this->pass = $pass;
}
public function Conecta(){
try {
new PDO($this->sql,$this->user,$this->pass);
}catch(Exception $e) {
var_dump($e);
}
}
public function getList() {
$this->lista = $this->Conecta()->prepare('SELECT * FROM outros');
$this->lista->execute();
$this->lista->fetchAll();
}
}
$testando = new Conexao;
$testando->Conecta();
$testando->getList();