问题
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