Is it wrong to return $this->something to another value after using the value of a previous assignment?

Asked

Viewed 19 times

1

I have the following example:

<?php

class Classe {

   public function calcula(){
      $resultado = ($this->valor * 2);
      $this->valor = $resultado;
      return $resultado;
   }

   public function valor(int $valor = 10){
      $this->valor = $valor;
      return $this;
   }

}

$classe = new Classe;

echo $classe->valor(2)->calcula();
echo '<br>';
echo $classe->calcula();
echo '<br>';
echo $classe->calcula();
echo '<br>';
echo $classe->calcula();

Example in ideone.

I will not use this for anything, in the method that I ended up falling in this context I managed to solve otherwise, but I was in doubt because it seemed wrong to me:

If this is a pro solution I need, it’s a wrong practice to do this in my code ?

  • 1

    maybe you don’t start $this->valor an error occurs, otherwise I believe not. example https://ideone.com/fork/ry4N8r

1 answer

3


It is not easier to use the __construct, young man?

In your case I would make two modifications: I would add a constructor leaving a default value for the property. And set the property to protectedor privada so it won’t be externally altered.

class Classe {

    protected $valor = 0;

    public function __construct(int $valor = 0)
    {
        $this->valor = $valor;        
    }

}

Now I believe your doubt is regarding the method Classe::calcula...

Answer: I do not believe it is advantageous to do this in case of a method that will calculate. If it will calculate, it should return the value, and not change the base value (the property $valor).

I would leave the method calcula thus:

public function calcula(): int
{
    return $this->valor * 2;
}

Now one thing you should ask yourself is not "whether it is good practice or not", but whether it will be useful or not to modify the value $valor.

You should think that there are several project standards just to solve common implementation problems (although you shouldn’t stick to that) and that each situation implies a different implementation.

Just out of curiosity, a form that could be applied in the above case (I see much of this implementation of the Laravel Collection class) is the immutability pattern, which consists of returning an instance of the class itself with the value of calcula. In that case I would modify the method valor to just return a value.

See how it could be applied:

class Classe {

    public function __construct(int $valor = 0)
    {
        $this->valor = $valor;        
    }

    public function calcula()
    {
        return new static($valor * 2);
    }

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

}

Use:

   $calculo[0] = new Calcula(20);

   $calculo[0]->valor(); // 20

   $calculo[1] = $calculo[0]->calcula(); // Object(Classe)

   $calculo[1]->valor(); // 40

   $calculo[2] = $calculo[0]->calcula()->calcula(); // Object(Classe)
  • I fell into a context where I needed to call two sequences of methods of the same class, but one for example I would call $class->methodo1('a')->metodo2('b')->vai(), and the other need to call $class->metodo2('b')->vai() but, in ->vai() it would pull the value of ->metodo1() defined earlier so I needed to go back $this->method1 = 'default' in ->vai() and return $this. Only in the example there I wanted to demonstrate in a more practical way, but it is not a method of calculation where falls into it.

Browser other questions tagged

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