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:
- Does it make sense to have an instance of a class without calling
f1() ? - Does the call to
f2() make sense outside the context of the state of the object (processing, say, a scalar value at the input)? - 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() ? - 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 .
$dataand store the result of the first function there$this->data = 123765123651237;; and in the second you use this variable - Alexey Shimansky$data; $this->data = $this->f1();- Islamprivate $data = null;........ first functionfunction f1(){ $v; //some code $this->data = $v; }function f1(){ $v; //some code $this->data = $v; }function f1(){ $v; //some code $this->data = $v; }..... secondfunction 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