Notice: a non well formed numeric value encountered

I created a fictitious example of a discount on a person's salary, the code works, but two notices appear that I listed below the Code:

    class Descontos{

        public $salario;
        public $inss;
        public $salarioLiquido;

        public function calcularPorcentagemINSS():float{
            if($this->salario < 1693.62 ){
                $this->inss = 8/100;
            } else if( $this->salario > 1693.63 OR $this->salario < 2822.90 ) {
                $this->inss = 9/100;
            } else {
                $this->inss = 11/100;
            }

            return $this->inss;
        }

        public function calcularValorINSS(){
            return $this->inss * $this->salario;
        }

        public function calcularSalarioLiquido(){
            return $this->salario - $this->calcularValorINSS();
        }

    }

    # INSTÂNCIA DA CLASSE
    $salario = new Descontos();

    echo "Salário bruto: " . $salario->salario = 2000 . "<br/>";

    echo "Porcentagem INSS: " . $salario->calcularPorcentagemINSS() * 100 . "% <br/>";

    echo "Valor de desconto INSS: " . $salario->calcularValorINSS() . "<br/>";

    echo "Salário Líquido: " . $salario->calcularSalarioLiquido();

Displayed Notices:

Notice: a non well formed numeric value encountered in C:\xampp\htdocs\18 -POO \ 06-class.php on line 23

Notice: a non well formed numeric value encountered in C:\xampp\htdocs\18 -POO \ 06-class.php on line 27

How can I solve them?

Author: Maniero, 2019-02-07

1 answers

When is doing

$salario->salario = 2000 . "<br/>"

Is converting the entire value to string and it is being saved in the variable. Since there is a concatenation it will turn "2000 <br/>", so it does not do what it expects. To solve this would have to do the separate inline assignment, as everyone does, or at least use parentheses in the proper place:

echo "Salário bruto: " . ($salario->salario = 2000) . "<br/>";

I put on GitHub for future reference.

In addition to this, there are other technical problems, even in the creation of class. And there are conceptual errors in the use of object orientation. And I haven't even spoken yet about the need for a class for this. One thing I've been talking about a lot and everyone ignores is that there's a lot of concern about using OOP, but little about making code right which is what matters. And most of the time right code has nothing to do with OOP, on the contrary, it is common for Code to be more right without OOP. Object orientation is difficult and even experienced people have a hard time doing it right. So, if you do not have much advantage should avoid its use, especially until you master the paradigm very well (which requires deep study, and nowadays Rare who wants to do this, people are just pretending that they make code right, so almost everything you see fails a lot on the web).

 0
Author: Maniero, 2020-08-27 13:18:27