Code stating which method does not exist but method is in Class

Asked

Viewed 26 times

0

I have the array down that comes from a consultation to the database using a class:

Array
(
    [0] => Estudos Object
        (
            [idEstudo:Estudos:private] => 12
            [assunto:Estudos:private] => Teste
            [estudo:Estudos:private] => Testando...
            [data:Estudos:private] => 2019-01-28
            [dataEstudo:Estudos:private] => 0000-00-00
        )

    [1] => Estudos Object
        (
            [idEstudo:Estudos:private] => 13
            [assunto:Estudos:private] => Dia D
            [estudo:Estudos:private] => Dia de brincar
            [data:Estudos:private] => 2019-02-25
            [dataEstudo:Estudos:private] => 0000-00-00
        )

    [2] => Estudos Object
        (
            [idEstudo:Estudos:private] => 14
            [assunto:Estudos:private] => Perizes
            [estudo:Estudos:private] => Perdição total?
            [data:Estudos:private] => 2019-02-26
            [dataEstudo:Estudos:private] => 2019-02-26
        )

    [3] => Estudos Object
        (
            [idEstudo:Estudos:private] => 15
            [assunto:Estudos:private] => Estudo
            [estudo:Estudos:private] => Estudando...
            [data:Estudos:private] => 2019-02-26
            [dataEstudo:Estudos:private] => 2019-02-27
        )

    [4] => Estudos Object
        (
            [idEstudo:Estudos:private] => 16
            [assunto:Estudos:private] => Estudo de Células
            [estudo:Estudos:private] => Esse estudo vai bombar
            [data:Estudos:private] => 2019-04-12
            [dataEstudo:Estudos:private] => 2019-04-12
        )

)

To class responsible for these objects is the following:

<?php

class Estudos {

    private $idEstudo; 
    private $assunto; 
    private $estudo; 
    private $data; 
    private $dataEstudo; 

    public function __construct($_assunto, $_estudo, $_data, $_dataEstudo) {

        $this->assunto = $_assunto;
        $this->estudo = $_estudo;
        $this->data = $_data;
        $this->dataEstudo = $_dataEstudo;

    }


    public function setIdEstudo ($_idEstudo) {

        $this->idEstudo = $_idEstudo;

    }


    public function getIdEstudo () {

        return $this->idEstudo;

    }


    public function getAssunto () {

        return $this->assunto;

    }


    public function getEstudo () {

        return $this->estudo;

    }

    public function getData () {

        return $this->data;

    }

    public function getDataEstudo () {

        return $this->dataEstudo;

    }

}


?>

That one array of objects of that class comes from this method:

public
function listar( $estudosDao, $phpUtil ) {

    $estudos = $estudosDao->pesquisaEstudos();

    $lista = null;

    if ( $estudos != null ) {

        $lista = "<h1 class='hCentralizado'>Listagem de Estudos</h1>";

        $lista .= "

            <ul class=listaTopo id=listaTopo>
                   <li style='width: 20%;'>Data do Estudo</li><!--
                   <li style='width: 25%;'>Assunto</li><!--
                --><li style='width: 25%;'>Estudo</li><!--
                --><li style='width: 15%;'>Editar</li><!--
                --><li style='width: 15%;'>Excluir</li>
            </ul>

        ";

        $pagina = isset( $_GET[ "pagina" ] ) ? $_GET[ "pagina" ] : 1;

        $regTotal = count( $estudos );
        $regPagina = 5;
        $limite = ceil( $regTotal / $regPagina );
        $inicio = ( $pagina * $limite ) - $limite;
        $fim = ( $pagina * $limite );


        $limitacao = "LIMIT " . $inicio . "," . $fim;
        $estudosPag = $estudosDao->pesquisaEstudos( $limitacao );

        $contador = 0;

        foreach ( $estudosPag as $estudo ) {

            $backGround = $contador % 2 == 0 ? "#CCC" : "#GGG";

            $linkPagina = isset( $_GET[ "pagina" ] ) ? "&pagina=" . $_GET[ "pagina" ] : "";

            $assunto = strlen( $estudo->getAssunto() ) >= 20 ? substr( $estudo->getAssunto(), 0, 20 ) . "...": $estudo->getAssunto();
            $estudo = strlen( $estudo->getEstudo() ) >= 20 ? substr( $estudo->getEstudo(), 0, 20 ) . "...": $estudo->getEstudo();
            $dataEstudo = $phpUtil->formataData( $estudo->getDataEstudo());

            $iconeVer     = "<img src='_imgs/editar.png'>";             
            $linkVer      = "<a href='?editarEstudos&form&idEstudo=" . $estudo->getIdEstudo(). "'>" . $iconeVer . "</a>";       

            $iconeExcluir = "<img src='_imgs/excluir.png' style='width: 40px;'>";
            $linkExclui = "<a href='?editarEstudos&excluir" . $linkPagina . "&idEstudo=" . $estudo->getIdEstudo() . "'>" . $iconeExcluir . "</a>";

            $lista .= "

                <ul class=listaRegistros style='background-color:" . $backGround . "'>
                       <li style='width: 20%'>" . $dataEstudo . "</li><!--
                    --><li style='width: 25%'>" . $assunto . "</li><!--
                    --><li style='width: 25%'>" . $estudo . "</li><!--
                    --><li style='width: 15%'>" . $linkVer . "</li><!--
                    --><li style='width: 15%'>" . $linkExclui . "</li>
                </ul>

            ";

            $contador++;

        }

        $pa = $pagina - 1;
        $pp = $pagina + 1;

        if ( $pagina == 1 ) {

            $anterior = "<a href='?editarEstudos&listar&pagina=" . $limite . "'>Anterior</a>";
            $proximo = "<a href='?editarEstudos&listar&pagina=" . $pp . "'>Próxima</a>";

        }

        if ( $pagina > 1 ) {

            $anterior = "<a href='?editarEstudos&listar&pagina=" . $pa . "'>Anterior</a>";
            $proximo = "<a href='?editarEstudos&listar&pagina=" . $pp . "'>Próxima</a>";

        }

        if ( $pagina == $limite ) {

            $pa = $limite == 1 ? 1 : $pa;

            $anterior = "<a href='?editarEstudos&listar&pagina=" . $pa . "'>Anterior</a>";
            $proximo = "<a href='?editarEstudos&listar&pagina=1'>Próxima</a>";

        }

        $botoes = "<div class=hCentralizado style='width: 200px;'>" . $anterior . "  // " . $proximo . "</div>";

        $lista .= $limite == 1 ? "" : $botoes;        

    }

    return $lista;

}

That’s the error that I’m getting:

<br />
<b>Fatal error</b>:  Uncaught Error: Call to a member function getDataEstudo() on string in D:\Trabalhos\host\htdocs\wesleyanagceu.com.br\_classes\_mvc\_vistas\EstudosVistas.php:50
Stack trace:
#0 D:\Trabalhos\host\htdocs\wesleyanagceu.com.br\admin\_editar\estudo.php(7): EstudosVistas-&gt;listar(Object(EstudosDao), Object(PhpUtil))
#1 D:\Trabalhos\host\htdocs\wesleyanagceu.com.br\adminConteudo.php(202): require_once('D:\\Trabalhos\\ho...')
#2 D:\Trabalhos\host\htdocs\wesleyanagceu.com.br\admin.php(71): require_once('D:\\Trabalhos\\ho...')
#3 {main}
  thrown in <b>D:\Trabalhos\host\htdocs\wesleyanagceu.com.br\_classes\_mvc\_vistas\EstudosVistas.php</b> on line <b>50</b><br />

The error, as I understood the message, is saying that the class does not possess the methods down below:

$estudo->getIdEstudo()
$estudo->getDataEstudo()

But the class does have these methods.

Where can I be missing?

  • Lines before you did $estudo = strlen(...), which overwrites its original object by a string.

  • in this case I am handing this change only one variable. This change is perpetual? If yes. , What can I do in this case?

  • No, but it will be string until the end of the loop and you tried to access the object’s methods when it was already a string. What you can do is define different names to avoid conflict.

  • got now created a variable with the same object name!

1 answer

1


On that line you are transforming the $estudo in a string:

$estudo = strlen( $estudo->getEstudo() ) >= 20 ? substr( $estudo->getEstudo(), 0, 20 ) . "...": $estudo->getEstudo(); 

That is why you are giving the error because there is no function in a string, use another variable name.

  • 1

    truth this variable should not have that name which is already the name of the objects. It was that same!

Browser other questions tagged

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