1 | <?php |
||
14 | class Timer { |
||
15 | public const FORMAT_PRECISE = FALSE; |
||
16 | public const FORMAT_MILLISECONDS = 'ms'; |
||
17 | public const FORMAT_SECONDS = 's'; |
||
18 | public const FORMAT_HUMAN = 'h'; |
||
19 | |||
20 | /** |
||
21 | * Stores all the timers statically. |
||
22 | * @var Stopwatch[] |
||
23 | */ |
||
24 | static private $timers = []; |
||
25 | |||
26 | /** |
||
27 | * Start or resume the timer. |
||
28 | * |
||
29 | * Call this method to start the timer with a given key. The default key |
||
30 | * is "default", and used in @see \Ayesh\PHP_Timer\Timer::read() and reset() |
||
31 | * methods as well |
||
32 | * |
||
33 | * Calling this with the same $key will not restart the timer if it has already |
||
34 | * started. |
||
35 | * |
||
36 | * @param string $key |
||
37 | */ |
||
38 | public static function start(string $key = 'default'): void { |
||
46 | |||
47 | /** |
||
48 | * Resets a specific timer, or default timer if not passed the $key parameter. |
||
49 | * To reset all timers, call @see \Ayesh\PHP_Timer\Timer::resetAll(). |
||
50 | * @param string $key |
||
51 | */ |
||
52 | public static function reset(string $key = 'default'): void { |
||
55 | |||
56 | /** |
||
57 | * Resets ALL timers. |
||
58 | * To reset a specific timer, @see \Ayesh\PHP_Timer\Timer::reset(). |
||
59 | */ |
||
60 | public static function resetAll(): void { |
||
63 | |||
64 | /** |
||
65 | * Formats the given time the processor into the given format. |
||
66 | * @param $value |
||
67 | * @param $format |
||
68 | * @return string |
||
69 | */ |
||
70 | private static function formatTime($value, $format): string { |
||
86 | |||
87 | /** |
||
88 | * Returns the time elapsed in the format requested in the $format parameter. |
||
89 | * To access a specific timer, pass the same key that |
||
90 | * @see \Ayesh\PHP_Timer\Timer::start() was called with. If the timer was not |
||
91 | * started, a \LogicException will be thrown. |
||
92 | * |
||
93 | * The default format is milliseconds. See the class constants for additional |
||
94 | * formats. |
||
95 | * |
||
96 | * @param string $key The key that the timer was started with. Default value is |
||
97 | * "default" throughout the class. |
||
98 | * @param string $format |
||
99 | * @return mixed The formatted time. |
||
100 | * @throws \LogicException |
||
101 | */ |
||
102 | public static function read(string $key = 'default', $format = self::FORMAT_MILLISECONDS) { |
||
108 | |||
109 | /** |
||
110 | * Stops the timer with the given key. Default key is "default" |
||
111 | * |
||
112 | * @param string $key |
||
113 | * |
||
114 | * @throws \LogicException If the attempted timer has not started already. |
||
115 | */ |
||
116 | public static function stop($key = 'default'): void { |
||
123 | |||
124 | /** |
||
125 | * Return a list of timer names. Note that resetting a timer removes the timer. |
||
126 | * @return string[] |
||
127 | */ |
||
128 | public static function getTimers(): array { |
||
131 | } |
||
132 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: