Undefined method error in table does not return getteres methods

Asked

Viewed 22 times

-2

I was making some changes in my code, changes that were the visibility of class attributes (I changed the attributes from public to private and am using getters and setters methods)

Only that the following error is occurring in the Tabela de listagem.

Fatal error: Uncaught Error: Call to a member function getAluno() on array
  • Class Aluno
abstract class Aluno extends BD {
        
        protected $tabela;
        private $cd_aluno, $nome, $endereco;

        public function getAluno() {
            return $this->cd_aluno;
        }

        public function getNome() {
            return $this->nome;
        }

        public function getEndereco() {
            return $this->endereco;
        }

        public function setAluno($cd_aluno) {
            $this->cd_aluno = $cd_aluno;
        }

        public function setNome($nome) {
            $this->nome = $nome;
        }
        
        public function setEndereco($endereco) {
            $this->endereco = $endereco;
        }
    }
  • Class CrudAluno
class CrudAluno extends Aluno{

   protected $tabela = 'aluno'; 

   public function Select() {
     $sql = "SELECT * FROM $this->tabela";
     $stm = BD::prepare($sql);
     $stm->execute();
     return $stm->fetchAll(PDO::FETCH_ASSOC);
   }
}
  • Listing table
<table id="lista" border="1">
        <tr> 
            <th> ID </th>
            <th> Nome </th>
            <th> Endereço </th>
            <th> Ações </th>
        </tr>
        <?php 
            foreach ($aluno->Select() as $key){
                echo '<tr>';
                echo '<td>'.$key->getAluno().'</td>';
                echo '<td>'.$key->getNome().'</td>';
                echo '<td>'.$key->getEndereco().'</td>';
                echo '<td>'."<a href='/crud/formulario/form_atualizar.php/#atualizar'>Atualizar</a> ".
                "<a href='/crud/formulario/form_excluir.php/#excluir'>Excluir</a>".'</td>';
                echo '</tr>'; echo '</p>';
            }
        ?>
    </table>
  • $aluno->Select() returns a array of arrays. https://www.php.net/manual/en/pdostatement.fetchall.php

1 answer

1


When does the foreach:

foreach ($student->Select() as $key)

What is named as key is actually value, at the moment every $key is the array with the content of a line in the BD;

If you want to get the key too (index in the count of the rows of the results), you should do:

foreach ($student->Select() as $key => $value)

But that’s not the problem yet, the error happens because it’s trying to run getAluno() as if it’s inside a result in the database, when in fact that function is in a class you wrote, it does not communicate automatically in this way.

Within this foreach you don’t need to use the class functions to get the data you can simply do:

<?php
foreach ($aluno->Select() as $value){
    //...
    echo '<td>'.$value['nomeDaColunaNoDb'].'</td>';
    //...
}

OBS.: When using the parameter FETCH_ASSOC to get the data, you will get array instead of object, so you must use $meuArray['chave'], to obtain some data and not ->.

If you want access to your class functions Aluno when using the Select class DB, your method Select cannot return the results, it must organize them in the class and return $this or an object that can use methods from the results, but it is necessary to create a logic to manage these various results, for example using an instantiable class for each result instead of abstract and return the class after using the methods of defining the data.

Browser other questions tagged

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