Uncaught Argumentcounterror in construction

Asked

Viewed 355 times

0

I’m trying to list the database data, only when I try to list it returns the error:

Uncaught Argumentcounterror: Too few Arguments to Function processes::__Construct(), 0 passed in

When I declare the variables in $mostra = new processa(*); it returns me the following error:

Undefined variable: name in

My index code:

<div class="container">

    <?php $mostra = new processa($nome, $email, $telefone); 
    $mostra->listaru($conn); 
    if (!empty($mostra)): ?>

        <table class="table">
            <thead class="thead-dark">
                <tr align="center">
                    <th scope="col">Nome</th>
                    <th scope="col">Email</th>
                    <th scope="col">Telefone</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($row = $mostra->fetch_assoc()): ?>
                    <tr>
                        <td>
                            <?= $row['nome'] ?>
                         </td>
                        <td>
                            <?= $row['email'] ?>
                        </td>
                        <td>
                            <?= $row['telefone'] ?>
                        </td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>

    <?php endif; ?>
</div>

php classes.:

class processa {
    private $nome, $email, $telefone;

    public function __construct($nome, $email, $telefone) {
        $this->nome = $nome;
        $this->telefone = $telefone;
        $this->email = $email;
    }
}

class processa {
    private $nome, $email, $telefone;

    public function __construct($nome, $email, $telefone) {
        $this->nome = $nome;
        $this->telefone = $telefone;
        $this->email = $email;
    }

    public function listaru($conn) {

        $puxar = "SELECT * FROM usuario;";

        return $conn->query($puxar);
    }
}

He comes to show the <thead> table, just does not show the contents of the <tbody>.

  • in Processa you are required to inform the three arguments in the constructor.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

4

The error message already tells you what the problem is, you create a constructor with 3 parameters:

public function __construct($nome, $email, $telefone) {

And calls him with zero arguments:

$user = new processa();

I put in the Github for future reference.

If the function was created with 3 you need to call with 3.

Actually I see other problems in the code, although it works. My recommendation would be not to do OOP until I understand what it is. OOP without understanding only makes the code worse, as is the case. One class is unnecessary, the other is too simple to be a class, and even if you insist on it, the name shows that it is not useful or what it should be.

Browser other questions tagged

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