PHP static delay binding vs. normal static efficiency
It's just a simple little experiment that contrasts the latency-bound and non-delayed efficiencies
Lazy binding is mostly the use of the static keyword to replace the original self, but the function is very powerful
Experiment Code:
Class A {protected static $CC 1 = Array (' A1 ', ' B ', ' C ', ' d '); protected static $CC 2 = Array (' A2 ', ' B ', ' C ', ' d '); protected static $CC 3 = Array (' A3 ', ' B ', ' C ', ' d '); protected static $CC 4 = array (' A4 ', ' B ', ' C ', ' d '); protected static $CC 5 = Array (' A5 ', ' B ', ' C ', ' d '); public static function N1 () {return static:: $CC 1; } public static function N2 () {return static:: $CC 2; } public static function N3 () {return static:: $CC 3; } public static function N4 () {return static:: $CC 4; } public static function N5 () {return static:: $CC 5; }} class C extends A {} class B {protected static $CC 1 = Array (' A1 ', ' B ', ' C ', ' d '); protected static $CC 2 = Array (' A2 ', ' B ', ' C ', ' d '); protected static $CC 3 = Array (' A3 ', ' B ', ' C ', ' d '); protected static $CC 4 = array (' A4 ', ' B ', ' C ', ' d '); protected static $CC 5 = Array (' A5 ', ' B ', ' C ', ' d '); public static function N1 () {Return to self:: $CC 1; } public static function N2 () {return self:: $CC 2; } public static function N3 () {Return to self:: $CC 3; } public static function N4 () {Return to self:: $CC 4; } public static function N5 () {Return to self:: $CC 5; } }
A total of a, B, C three classes, which are all static member variables and methods, where
The Class A uses a static delay,
Class B is non-delayed,
Class C is a deferred binding that inherits the Class A to implement static member variables and methods.
The process is not much to say, the environment is PHP5.4.27 directly on the test results:
There are two situations,
1. When there is only a, B class (That is, no class inherits from Class A), there is almost no difference in efficiency
2. When Class A is inherited by Class C, the class A performance using static delay binding will be slightly worse than Class B (as long as Class A has inherited classes will become slower)
Cycle 100,000 times, time-consuming between 2.8s ~ 3.2s gap in 0.3 seconds or so, it should still be negligible
add: later added a few test methods, if class C inherits the Class A, the static member variables in the Class A are overloaded, the more overloaded the speed and Class B (non-delay) the closer, but class A is still slower than Class B and Class C