Notice: A non well Formed Numeric value encountered

Asked

Viewed 2,108 times

0

I created a fictitious example of a discount on a person’s salary, the code works, but two appear notices 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();

Notices presented:

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 fix them?

1 answer

0


When you’re doing

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

is converting all the value into string and it is being stored in the variable. As there is a concatenation it will turn "2000 <br/>", so don’t do what you expect. To solve this would have to do the separate online assignment, as everyone does, or at least use parentheses in the appropriate location:

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

I put in the Github for future reference.

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

  • Thanks for the solution presented and for the tips. I started studying Object Oriented Programming today, in which cases you would advise me to use this style of programming?

  • In those extremely complex, ie in PHP does not make sense in almost all cases. Actually, I don’t know how much use it is for me to say this because almost all the material you have out there is teaching OOP wrong.

Browser other questions tagged

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