1

i have a question about PHP performance. I'm using my class methods very often in one instance. For example i have method for calculation a starting time of production shift. Calculation can be used just once, because for all calculations is the same start time.

Analysis tool: xdebug (this function is called mostly and takes longest time)

What is best way how to perform script? I dont wanna make a lot of static (about 20) variables.. Or best way is using static functions for this case?

Now for very often called functions im using:

class test{ private $data; public function __construct () { $this->data=0; } public function foo() { if($this->data === 0) { // some code $this->data=$result; } return $this->data; } } 

I think this isnt good way how to solve my problem. Thanks you for your time.

2
  • 1
    "I think this isnt good way"....because? What's your specific concern? Commented Feb 3, 2020 at 13:21
  • I didn't get the question itself :( Commented Feb 3, 2020 at 13:22

1 Answer 1

2

Using setter functions for setting object properties is the right way to go. Only difference I would make is removing the return from foo() function and just accessing the property directly when needed.

 public function foo() { // some code $this->data=$result; } 

Now after calling foo() once, you can use the $this->data to access the result every time without calling the function again and doing unnecessary calculations.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.