Demand that one method must be followed by another?

Asked

Viewed 62 times

2

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

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 will get 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 it in the following way, making two calls, in this case I do not have the desired result, because 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 a method is missing from the call?

Ex: in the case, result() should not respond successfully if metodoY() were not called.

According to:

It is possible to declare compulsory ordering in the execution of methods in a class?

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

  • If this is a rule, then wouldn’t it be better to call the Y method always within the X method before anything?

  • @Andersoncarloswoss It doesn’t end up being a fixed rule, the methods that I need are components of other methods, these will dictate the rules, but I wanted to prevent certain 'natural' things from proceeding, as for example in the second call, it recoverY method value of the first one, wanted to make it not happen by answering an error if it is possible natively.

  • 1

    Honestly, I don’t see any point in doing that. Is there any way to describe your real need? If the methods are of the same object, then it makes no sense that you do not use the same value as previously defined. For distinct objects, yes; then it would be enough to check that the value of Y is different from NULL, for example.

  • I think I expressed myself badly in the question, I can for example compare if it is null, if it is requesting the method, but what I wanted to know is if there are any rules, PHP native, to indicate that methodoX is necessarily dependent on methodY has been called before it in the same string.

  • The environment in question that I need is a group of PHP classes for constructing bootstrap elements, and I can for example: In a call I may want the button with attribute->active(true), and in the same instance of the class, but in a second object, I do not need to specify ->active(false), and yes, if I do not indicate anything that it automatically fetches the default value for ->active(), which in this case would be false, and render the element as ->active(false). I know I can do this within the methods, I just wanted to know if it’s possible by native rule.

1 answer

5


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

Having a valid state that soon becomes invalid feels weird and conceptually wrong. You can create a mechanism that indicates this, but it doesn’t seem right.

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

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

Depending on the case you can keep the initial value null and only allow calculations when it is not null. If this is not possible you can keep flags indicating whether the value was setate previously and there it can be used. It is a huge gambiarra. It seems to solve a problem, but in fact it can even cause others. The right thing to do is to rethink the concept.

But then I guess it wouldn’t even work.

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

But in this particular case I don’t even see a solution because I want the object to no longer have a state that it had before. The way to do this is to create another object. Do the 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 what is worth ceases to be true.

If the rules are variables of 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) indicating how the behaviour should be in that case.

From what I understand, you don’t even want to have an object. That’s what I always say, it’s common for people to want to do object-oriented things that shouldn’t be. If you do not want to maintain status, do not create an object.

So the question starts from a wrong premise.

But answering objectively has no way to require call order in the established form in the question that is too complicated even to maintain flags.

Solve the problem the right way and you won’t 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 makes sense to have something like this, but to solve in general it needs a workflow framework, what is not the case.

  • That’s exactly what I wanted to know, what I want to do would have no way to work if it is not possible to give a mandatory order to the methods, I will look for another way to do this.

  • I couldn’t have answered it better.

Browser other questions tagged

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