Calling the variable property directly vs getter/setters - OOP Design

后端 未结 7 866
-上瘾入骨i
-上瘾入骨i 2021-01-17 09:31

I know this is probably subjective but I read this optimization page from Google for PHP and they suggest use the variable property directly without the need of getters and

7条回答
  •  没有蜡笔的小新
    2021-01-17 10:19

    At first I was surprised, I was like ... wtf. But after wrapping my brain on it a few seconds, I realized the example calls the getter function 1 million time in a loop. Of course if the variable is wrapped in a getter, we have added instructions and of course it's going to take longer.

    In my opinion in most situations this is very trivial because I have yet to come accross a script that comes event close to calling getters 1 million time when running. If you do need to squeeze performance to the very last drop, it is good to know this optimisation technique.

提交回复
热议问题