Error: Object of class Student could not be converted to string

Asked

Viewed 16 times

0

I am with a small doubt, I was making a web application in PHP that simulated a student, displaying his information as his name, enrollment, note 1, note 2 and the average of these notes, but when I will put to display the average appears the error described in the title of this post, thanks if you can help (the error happens on line 24 of the index.php code, but I will leave the code of the other classes)

index php.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Teste</title>
    </head>
    <body>
        <?php
        require_once 'Aluno.php';
        
        $aluno = new Aluno();
        
        $aluno->nome = 'Paulo';
        $aluno->matricula = '20210827';
        $aluno->nota1 = 9;
        $aluno->nota2 = 8;
        
        echo 'Nome: '.$aluno->nome;
        echo '<br>Matricula: '.$aluno->matricula;
        echo '<br>Nota 1: '.$aluno->nota1;
        echo '<br>Nota 2: '.$aluno->nota2;
        echo '<br><br>';
        
        echo 'Média: '.$aluno.getMedia();
        ?>
    </body>
</html>

Pupil.php

<?php

require 'Calculadora.php';

class Aluno {
    public $nome;
    public $matricula;
    public $nota1; // valor padrão = 0
    public $nota2; // valor padrão = 0
    
    function getMedia() {
        $calc1 = new Calculadora($this->valor1, $this->valor2);
        
        $calc1->valor1 = $this->nota1;
        $calc1->valor1 = $this->nota2;
        
        $calc1->getDivisao();
    }
}

Calculator.php

<?php

class Calculadora {
    public $valor1;
    public $valor2;
    
    function __construct($valor1, $valor2) {
        $this->valor1 = $valor1;
        $this->valor2 = $valor2;
    }
    
    function getValor1() {
        return $this->valor1;
    }

    function getValor2() {
        return $this->valor2;
    }
    
    function getSoma() {
        return $this->valor1 + $this->valor2;
    }
    
    function getSubtracao() {
        return $this->valor1 - $this->valor2;
    }
    
    function getMultiplicacao() {
        return $this->valor1 * $this->valor2;
    }
    
    function getDivisao() {
        return $this->valor1 / $this->valor2;
    }
}

1 answer

0

Your error is in the last line. Note that you used the dot (.), the string concatenation operator. With this, the interpreter tried to convert the object to string in order to concatenate with the rest:

        echo 'Média: '.$aluno.getMedia(); //aqui $aluno.getMedia()

To resolve, you must change by ->.

        echo 'Média: '.$aluno->getMedia(); //deixe assim

Taking the opportunity, you can overwrite the method __toString() and with it, program the class to have a representation that is useful in those moments. For example:


class Aluno {
    public $nome;
    public $matricula;
    public $nota1; // valor padrão = 0
    public $nota2; // valor padrão = 0
    
    function getMedia() {
        //o código da média
    }

    public function __toString(){
        // retorna uma representação textual do OBJETO do tipo Aluno atual
        // exemplo: 'Aluno: [007] James Bond'
        return 'Aluno: ['. $this->matricula.'] ' . $this->nome
    }
}

Browser other questions tagged

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