Why should we use static calls in PHP?

泄露秘密 提交于 2020-01-03 16:46:39

问题


Why should we use static variables or static calls to static methods in PHP5? Maybe to improve performance?


回答1:


We use static class variables to share data between all the instances of the class, and we use static methods (preferably private static) to compute something required for the class functionality, but independent of the class instance state ($this).

Performance is really not the reason for the existence of static-s. It's more like a side effect.




回答2:


Using static classes allows you to better organise code and functions that don't need to be represented by it's own instance. For example factory classes, helper classes, ulitily classes etc.

So for example, you could have a set of utility functions that manipulate numbers. Putting these in a static class "Math" allows you to group them together.




回答3:


With static calls you don't need to make an instance of the class so you save some memory if you don't need an actual object.



来源:https://stackoverflow.com/questions/1299712/why-should-we-use-static-calls-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!