1 | <?php |
||
27 | class NsCache |
||
28 | { |
||
29 | |||
30 | const FileName = '_ns.php'; |
||
31 | |||
32 | private $file = ''; |
||
33 | |||
34 | /** |
||
35 | * Addendum instance |
||
36 | * @var Addendum |
||
37 | */ |
||
38 | private $ad = null; |
||
39 | |||
40 | /** |
||
41 | * |
||
42 | * @var |
||
43 | */ |
||
44 | private static $nsCache = []; |
||
45 | |||
46 | /** |
||
47 | * Hash map of namespaces. |
||
48 | * Namespaces is key, while value is boolean and is not really relevant |
||
49 | * @var array |
||
50 | */ |
||
51 | private $namespaces; |
||
52 | |||
53 | /** |
||
54 | * @internal Flag that will trigger cache validity check for namespaces cache |
||
55 | * @var bool |
||
56 | */ |
||
57 | public static $addeNs = true; |
||
58 | |||
59 | 50 | public function __construct($path, Addendum $addendum, MetaOptions $options = null) |
|
60 | { |
||
61 | 50 | $this->file = sprintf('%s/%s', $path, self::FileName); |
|
62 | 50 | $this->namespaces = $addendum->nameKeys; |
|
63 | 50 | $this->ad = $addendum; |
|
64 | 50 | $this->setOptions($options); |
|
65 | 50 | } |
|
66 | |||
67 | 54 | public function setOptions(MetaOptions $options = null) |
|
81 | |||
82 | 2 | public function valid() |
|
94 | |||
95 | 2 | public function get() |
|
96 | { |
||
97 | 2 | if (!empty(self::$nsCache[$this->file])) |
|
98 | 2 | { |
|
99 | 2 | return self::$nsCache[$this->file]; |
|
100 | } |
||
101 | self::$nsCache[$this->file] = SoftIncluder::includeFile($this->file); |
||
102 | return self::$nsCache[$this->file]; |
||
103 | } |
||
104 | |||
105 | 47 | public function set() |
|
117 | |||
118 | public function remove() |
||
126 | |||
127 | 2 | private function isValid($ns) |
|
128 | { |
||
159 | |||
160 | } |
||
161 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.