Place the function output

Asked

Viewed 51 times

0

ola I’m wanting to display my result of my calculator functionImc but this not going, could help me I’m kind of layman in php.

error that this giving when running the site: Notice: Undefined Property: Pessoa::$calcularImc in C: xampp htdocs pw1 class a_a 0608 exemploFormulario php classes Pessoa.php on line 69

Pessoa.php  
<?php  
class Pessoa
{
    private $nome;
    private $idade;
    private $peso;
    private $altura;

    public function setNome($nome)
    {
        $this->nome = $nome;
    }

    public function setIdade($idade)
    {
        $this->idade = $idade;
    }

    public function setPeso($peso)
    {
        $this->peso = $peso;
    }

    public function setAltura($altura)
    {
        $this->altura = $altura;
    }

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

    public function getIdade()
    {
        return $this->idade;
    }

    public function getPeso()
    {
        return $this->peso;
    }

    public function getAltura()
    {
        return $this->altura;
    }

    public function calcularImc($peso, $altura)
    {
        return $peso/($altura * $altura);
    }

    public function cadastrar($nome, $idade, $peso, $altura)
    {
        $this->nome = $nome;
        $this->idade = $idade;
        $this->peso = $peso;
        $this->altura = $altura;
    }

    public function exibir()
    {

        return"<b>Nome</b>: $this->nome<br>
               <b>Idade</b>: $this->idade<br>
               <b>peso</b>: $this->peso<br>
               <b>altura</b>: $this->altura<br>
               <b>imc</b>: $this->calcularImc<br>";
    }

}

?>

sistema.php

<?php  
if (isset($_POST['pagina'])) {
    include 'classes/pessoa.php';

    $p = new Pessoa();

    $p->cadastrar($_POST['nome'], $_POST['idade'], $_POST['peso'], $_POST['altura']);

    echo "<div>".$p->exibir()."</div>";
}
else{
    echo "teste";
}

echo '<br><br><a href="../index.php">Voltar<a>';

?>

  • You edited the question but did not give the feedback, It was error at the time of putting in the OS or was wrong even?

  • I returned the value and it still didn’t work

  • this error appears : "Notice: Undefined Property: Pessoa::$calcularImc in C: xampp htdocs exemploFormulario php classes Pessoa.php on line 69" And line 69 is where this: <b>imc</b>: $this->calcularImc<br>";

  • Put the error in the article, it will be easier to view. The error is here: $this->calcularImc You are running as if it were a property, but it is actually a method. Switch to $this->calculator()

  • I just did that, but is appearing the () as a result.

  • Try like this <b>imc</b>: ". $this->calcularImc()." <br>";

  • tried and gave the error: Fatal error: Uncaught Argumentcounterror: Too few Arguments to Function Pessoa::calcularImc(), 0 passed in C: xampp htdocs pw1 exemploFormulario php classes Pessoa.php on line 69 and Exactly 2 expected in C:xampp htdocs pw1 class For exampleFormulario php classes Pessoa.php:49 Stack trace: #0 C: xampp htdocs exemploFormulario php classes Pessoa.php(69): Pessoa->calcularImc() #1 C: xampp htdocs exemploFormulario php system.php(9): Pessoa->exibir() #2 {main} thrown in C: xampp htdocs exemploFormulario php classes Pessoa.php on line 49

  • thank you very much for helping but I managed to resolve with the reply of the Oberto

Show 3 more comments

1 answer

1


The error is in your calculator methodImc which takes 2 parameters and you are not passing. It has two solutions.

The first

Replace in your display method:

$this->calcularImc for {$this->calcularImc($this->peso, $this->altura)}

The second solution which is the most recommended

Modify your calculator method to use attributes:

public function calcularImc()
{
    return $this->peso/($this->altura * $this->altura);
}

And modify your display method:

$this->calcularImc for {$this->calcularImc()}

Browser other questions tagged

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