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?
– Woss
@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.
– AnthraxisBR
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.– Woss
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.
– AnthraxisBR
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.
– AnthraxisBR