1 | <?php |
||
18 | trait HasInstances |
||
19 | { |
||
20 | /** |
||
21 | * Cached instances |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected static $instances = array(); |
||
26 | |||
27 | /** |
||
28 | * Remove an instance from cache. |
||
29 | * |
||
30 | * @param integer $id Class identifier |
||
31 | * |
||
32 | * @return void |
||
33 | */ |
||
34 | 2 | public static function clearInstance($id) |
|
38 | |||
39 | /** |
||
40 | * Clear all instances from cache |
||
41 | * |
||
42 | * @return void |
||
43 | */ |
||
44 | 1 | public static function clearAllInstances() |
|
45 | { |
||
46 | 1 | unset(static::$instances[get_called_class()]); |
|
47 | 1 | } |
|
48 | |||
49 | /** |
||
50 | * Ensure that we retrieve a non-statically-cached instance. |
||
51 | * |
||
52 | * @param integer $id Identifier of the instance |
||
53 | * |
||
54 | * @return $this |
||
55 | */ |
||
56 | 1 | public static function freshInstance($id) |
|
62 | |||
63 | /** |
||
64 | * Create and return a cached instance |
||
65 | * |
||
66 | * @param integer $id Identifier of the instance |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | 4 | public static function instance($id) |
|
81 | } |
||
82 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.