Require that one method must be after another?

1

Let's say that I have a class with two methods, and the x method must be executed after the y method otherwise the x method must execute the y method to get the default value. / p>

class Classe 
{
   private $metodoX;
   private $metodoY;

   public function result(){
       $rs = $this->metodoX * $this->metodoY;
       return $rs;
   }

   public function metodoX(int $x = 1)
   {
      $this->metodoX = $x;
      return $this;
   }

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

If I call the methods as follows, I'll have one of the desired results:

$class = new Classe();

$data = $class->metodoY(3)->metodoX(2)->result();
echo $data;
//response = int 6

But if I want to use the following two calls, in this case I do not have the desired result, since the value of metodoY() of the first call is mirrored to the second call:

$class = new Classe();

$data = $class->metodoY(3)->metodoX(2)->result();
echo $data;
echo '<br>';
$data2 = $class->metodoX(2)->result();
echo $data2;
//response = (int) 6 <br> (int) 6

First:

  

How to prevent the class from continuing if there is any method missing from the call?

Ex: In this case, result() should not respond successfully if metodoY() was not called.

Second:

  

There is a way to declare mandatory ordering when executing methods   in a class?

Ex: Define that metodoX() should always be started after metodoY() , otherwise respond an error or in other situations, metodoX() execute metodoY() to fetch a default .

    
asked by anonymous 18.08.2017 / 15:43

1 answer

3

This is usually a conceptual error. An object should already be created in a valid state to perform any behavior at any time. For this builder . The valid status guarantee should ideally be made in the builder. And any object state change should also ensure that it is still in valid state.

Having a valid state that immediately becomes invalid seems bizarre and conceptually wrong. You can create a mechanism that indicates this, but it does not seem right.

If the data can change and either the same result should create another object (which seems to be the case by the comments). If you want to change the state of the object accept that it will not always work as expected, which obviously is not ideal.

If you still want to do this conceptually wrong in this way, you can even try to verify that the state is valid before executing the behavior. In some cases verification can be done within previously established rules, in others it needs some extra mechanism.

Depending on the case you can keep the initial value null and only let you do calculations when it is not null. If this is not possible you can keep flags indicating whether the value has been set previously and then it can be used. It's a huge gambiarra. It seems to solve one problem, but in fact it may even cause others. The correct thing is to rethink the concept.

But in this case it seems to me that this would not even work.

If you want a method that always has two arguments before, make this method have both arguments.

But in this specific case, I can not even see a solution because I want the object to be left alone to have a state it already had before. The way to do this is to create another object. Make result() receive the values and use them as you want. It is complicated to create an atomic form in different methods, even more that soon after that goes worthless.

If the rules are variable when you can do it one way or another then you should probably have different objects for each rule. Or be some kind of policy (some flag ) that indicates how the behavior should be in that case.

As far as I can understand or wish to have an object. It's what I always say, it's common for people to do object-oriented what they should not be. If you do not want to keep state, do not create an object.

So the question starts from a wrong premise. But objectively responding has no way to require call order in the form set in the question that is too complicated to even keep flags .

Solve the problem the right way and you will not have this difficulty. Trying to put a screw with a hammer may even give a result, but it will not be suitable and will be very difficult.

There is a situation where it even makes sense to have something like this, but to solve it in general you need a workflow framework , which is not the case.

    
18.08.2017 / 15:59