Fatal error: Uncaught Error: Call to undefined function listarUsers ()

0

I'm starting in PHP, and I'm having a problem using the . What I want to do is only a SELECT of all the users registered in the 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>

Username.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;
   }
 }

User.php

 <?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-users.php: 47 Stack trace: # 0 {main} thrown in C: \ xampp \ htdocs \ Admin-ame \ listar-users.php on line 47

This failure may be occurring because I did not call the class DAO in the file Listar-Usuarios.php?

    
asked by anonymous 07.11.2017 / 02:25

1 answer

0

Good,

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

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

You also need to define the variable $ connection that I think is connected to something in the conectadb.php     

07.11.2017 / 08:03