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 .