there is a class within which two functions. the first function returns the value of the variable that is used as the parameter for the second function

example:

class example{ function f1(){ $v; //some code retutn $v; } function f2($param){ echo $param; } } $obj = new example; $n = $obj -> f1(); $obj -> f2($n); 

this is how i was able to implement. but is it possible to do something like that

 class example{ function f1(){ $v; //some code retutn $v; } $n = f1(); function f2(){ echo $this -> n; } } $obj = new example; $obj -> f1(); $obj -> f2(); //и тут напечатался бы результат выполнения первой функции 

that is, is it possible to somehow transfer the parameter value already inside the class, so that when calling the second function, do not specify the input parameter?

  • It is not clear why not to immediately execute f2 (), and call f1 () in it? Or will there be a lot of functions like f1 () and it will be necessary for everyone to choose which of them to call? - cheops
  • @cheops, to be honest, I didn’t have such a thought in my head ... I think this is just what I need. because for each f2 () you need the result of the execution of f1 () of its own object. thanks - Islam
  • Get a property .... for example $data and store the result of the first function there $this->data = 123765123651237; ; and in the second you use this variable - Alexey Shimansky
  • @ Alexey Shimansky, that is, something like this? $data; $this->data = $this->f1(); - Islam
  • in the class private $data = null; ........ first function function f1(){ $v; //some code $this->data = $v; } function f1(){ $v; //some code $this->data = $v; } function f1(){ $v; //some code $this->data = $v; } ..... second function f2(){ echo $this->data; } function f2(){ echo $this->data; } ....... and everything else is unchanged $obj = new example; $obj -> f1(); $obj -> f2(); $obj = new example; $obj -> f1(); $obj -> f2(); ......... ideone.com/JtG4jq ....... and if you need для каждой f2() нужен результат выполнение f1() then you can do as cheops said. It will be more correct - Alexey Shimansky

1 answer 1

I want to draw your attention to one nuance, which is called temporal coupling . Many (most? All?) Methods of your class depend on the result of the f1() method. The call f2() must be preceded by the call f1() for the application to work correctly. Those. other developers who will work with your class need to constantly keep in mind this implementation detail, which, by the way, does not follow from the interface. Those. the developer will come to understand this order either by reading the documentation, or by trial and error, or by digging into the source code of your class. These are all signs of a bad design class. After all, it turns out that the class instance after its creation is not configured to run the methods f2() , f3() , etc. It is difficult to say something in isolation from a specific task, but I would first answer a number of questions:

  1. Does it make sense to have an instance of a class without calling f1() ?
  2. Does the call to f2() make sense outside the context of the state of the object (processing, say, a scalar value at the input)?
  3. Are the methods f2() , f3() etc. duty of this class? Or maybe the f1() method should return a properly configured object of another class that can already handle f2() and f3() ?
  4. Does the system value the result of the work f1() outside the context of the object state?

It may be reasonable to expect an argument for f1() in the constructor, and write the result in the object field, ensuring that when the other methods are called, the object will be configured. Maybe all the same it is necessary to carry out the decomposition of the class, delimiting the responsibilities, it can make the system more flexible and more expandable. Most likely this is overkill for your task, but I advise you to carry out this kind of analysis whenever you encounter such problems - this is a typical code smell, a sign of poor design, not to attach any importance to this when developing an important backbone module — with a high probability of increasing the future costs of supporting the system .

  • Thanks for the recommendation, I can not disagree with your words and take them into account in the future. This problem is solved, say, for fun, I fill cones. there will be no practical use of the class. and the essence, in fact, is that by f1 () I meant connecting to the server, and in by f2 () and f3 (), the choice of base and execution of the query. wrote his class of work with the database to reduce the number of parameters passed to the standard functions - Islam