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 | private const TIMES = [ |
||
21 | 'hour' => 3600000, |
||
22 | 'minute' => 60000, |
||
23 | 'second' => 1000, |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * Stores all the timers statically. |
||
28 | * @var Stopwatch[] |
||
29 | */ |
||
30 | static private $timers = []; |
||
31 | |||
32 | /** |
||
33 | * Start or resume the timer. |
||
34 | * |
||
35 | * Call this method to start the timer with a given key. The default key |
||
36 | * is "default", and used in @see \Ayesh\PHP_Timer\Timer::read() and reset() |
||
37 | * methods as well |
||
38 | * |
||
39 | * Calling this with the same $key will not restart the timer if it has already |
||
40 | * started. |
||
41 | * |
||
42 | * @param string $key |
||
43 | */ |
||
44 | public static function start(string $key = 'default'): void { |
||
52 | |||
53 | /** |
||
54 | * Resets a specific timer, or default timer if not passed the $key parameter. |
||
55 | * To reset all timers, call @see \Ayesh\PHP_Timer\Timer::resetAll(). |
||
56 | * @param string $key |
||
57 | */ |
||
58 | public static function reset(string $key = 'default'): void { |
||
61 | |||
62 | /** |
||
63 | * Resets ALL timers. |
||
64 | * To reset a specific timer, @see \Ayesh\PHP_Timer\Timer::reset(). |
||
65 | */ |
||
66 | public static function resetAll(): void { |
||
69 | |||
70 | /** |
||
71 | * Formats the given time the processor into the given format. |
||
72 | * @param float $value |
||
73 | * @param string|bool $format |
||
74 | * @return string |
||
75 | */ |
||
76 | private static function formatTime(float $value, $format): string { |
||
92 | |||
93 | private static function secondsToTimeString(float $time): string |
||
106 | |||
107 | /** |
||
108 | * Returns the time elapsed in the format requested in the $format parameter. |
||
109 | * To access a specific timer, pass the same key that |
||
110 | * @see \Ayesh\PHP_Timer\Timer::start() was called with. If the timer was not |
||
111 | * started, a \LogicException will be thrown. |
||
112 | * |
||
113 | * The default format is milliseconds. See the class constants for additional |
||
114 | * formats. |
||
115 | * |
||
116 | * @param string $key The key that the timer was started with. Default value is |
||
117 | * "default" throughout the class. |
||
118 | * @param string $format |
||
119 | * @return mixed The formatted time. |
||
120 | * @throws \LogicException |
||
121 | */ |
||
122 | public static function read(string $key = 'default', $format = self::FORMAT_MILLISECONDS) { |
||
128 | |||
129 | /** |
||
130 | * Stops the timer with the given key. Default key is "default" |
||
131 | * |
||
132 | * @param string $key |
||
133 | * |
||
134 | * @throws \LogicException If the attempted timer has not started already. |
||
135 | */ |
||
136 | public static function stop($key = 'default'): void { |
||
143 | |||
144 | /** |
||
145 | * Return a list of timer names. Note that resetting a timer removes the timer. |
||
146 | * @return string[] |
||
147 | */ |
||
148 | public static function getTimers(): array { |
||
151 | } |
||
152 |
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: