Fatal error: Uncaught Error: Call to Undefined Function listarUsuarios()

Asked

Viewed 1,300 times

0

I am starting in PHP and I am suffering with a problem when using the paradigm of . What I intend to do is just one SELECT of all users registered in mysql database and return to a page, I am using the following logic:

list-users.php

<?php
 require_once 'cabecalho.php';
 require_once 'conectadb.php';
require_once 'Usuario.php';
require_once 'UsuarioDAO.php';
?>
<!-- Listando os Usuarios-->
<table class="table">
    <thead>
        <tr>
            <th>Nome:</th>
            <th>E-mail:</th>
            <th>Estado:</th>
            <th>Cidade:</th>
            <th>Status:</th>
            <th>Permissao:</th>
            <th>Data do cadastro:</th>
            <th>Opções</th>
        </tr>
    </thead>
    <tbody>

        <tr>
            <?php
            $usuarios = listarUsuarios();
            foreach ($usuarios as $usuario) :
                ?>
                <td><?= $usuario->getNome() ?></td>
                <td><?= $usuario->getEmail() ?></td>
            </tr>

            <?php
        endforeach
        ?>
    </tbody>
</table>

User.php

require_once 'conectadb.php';
require_once 'Usuario.php';

 class UsuarioDAO {

 private $conexao;

  function __construct($conexao) {
    $this->conexao = $conexao;
  }
  public function listarUsuarios(){
    $usuarios = array();
    $resultado = mysqli_query($this->conexao, "SELECT * FROM usuario");
    while ($array = mysqli_fetch_assoc($resultado)) {
        $usuario = new Usuario();
        $usuario->setIdusuario($array['idusuario']);
        $usuario->setNome($array['nome']);
        $usuario->setEmail($array['email']);
        $usuario->setEstado($array['estado']);
        $usuario->setCidade($array['cidade']);
        $usuario->setStatus($array['status']);
        $usuario->setPermissao($array['permissao']);
        $usuario->setDatacadastro($array['datacadastro']);
        array_push($usuarios, $usuario);
    }
    return $usuarios;
   }
 }

Php user.

 <?php

class Usuario{

private $idusuario;
private $nome;
private $email;
private $cep;
private $estado;
private $cidade;
private $bairro;
private $telefone;
private $status;
private $permissao;
private $datacadastro;
private $ipcadastro;
private $senha;

 Getter and setters

  ...Codigo omitido...

The error presented by PHP is:

Fatal error: Uncaught Error: Call to Undefined Function listarUsuarios() in C: xampp htdocs Admin-ame listar-usuarios.php:47 Stack trace: #0 {main} thrown in C: xampp htdocs Admin-ame listar-usuarios.php on line 47

This failure may be occurring because I didn’t call the class DAO in the archive Listar-Usuarios.php?

  • 1

    You used $usuarios = new Usuariodao; I think that might be it

  • Missing instantiation of the object UsuarioDAO as @Matheuslopesmarques said.

1 answer

0


Good,

You are calling a function that is inside the object "User". you need to start a new instance.

$usuarioDAO = new UsuarioDAO($conexao);
$usuarios = $usuarioDAO->listarUsuarios();

also need to define the variable $connection I think it’s connected to something in the file conectadb.php

Browser other questions tagged

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