Why should we use static variables or static calls to static methods in PHP5? Maybe to improve performance?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With