Entity causing changes in values when called twice

I have this getter in an entity

public function _getOver(){
        return   ($this->montant_heure + $this->montant_materiel) - $this->montant_credit ;
    }

In a view, I Have this line somewhere in a foreach

<td><?= round($p->over, 2) ?></td>

It gets the value correctly Now, if I want to do this

<td <?php if($p->over < 0){echo "class=\"over-negatif\"";} ?>><?= round($p->over, 2) ?></td>

I realise that It does not just get the value … it executes something and now the values are incorrect.
I really would have tought of the code in the entity would have acted as a getter – and not change anything… what would be the correct way of doing this ?

Edit : setting a variable and then using it works

 <?php $Over = $p->over ?>  <!-- then use $Over intead of $p->over -->

But I wonder if there is a way of being sure the entity get something without setting anything.

I’m curious, to what value does “over” change, exactly? Is there some discernable logic behind it?

Your basic assumption should be right; You should be able to manipulate entity fields and call them however many times you like.

However, if you change a field that plays a role in the generation of your new field (i.e. montant_heure), it WILL change the result of “over” in future uses.

So if you were to echo $p->over, then change montant_heure and then call $p->over again, you would get a different result, since the calculation is done anew every time.

It’s possible that the round() function actually causes the change, though I’m not sure.

If that’s the case, you could always just create another entity such as overRounded instead of changing $p->over.
But putting them into variables might be a good idea performance-wise if you plan on using them often.