tags:

views:

173

answers:

3

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

+2  A: 

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.

easement
+4  A: 

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.

JonB
+8  A: 

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.

Ivan Krechetov