1 | <?php |
||
10 | trait DynamicObjectsTrait |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | protected static $dynamicMethods = array(); |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | protected static $dynamicProperties = array(); |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected static $cache = array(); |
||
26 | |||
27 | /** |
||
28 | * Add a dynamic property. |
||
29 | * |
||
30 | * @param string $name |
||
31 | * The property name. |
||
32 | * @param mixed $value |
||
33 | * The property value. |
||
34 | * @param bool $memoize |
||
35 | * Memoize parameter. |
||
36 | */ |
||
37 | 5 | public static function addDynamicProperty($name, $value, $memoize = false) |
|
45 | |||
46 | /** |
||
47 | * Add a dynamic method. |
||
48 | * |
||
49 | * @param $name |
||
50 | * The method name. |
||
51 | * @param \Closure $func |
||
52 | * The method. |
||
53 | * @param bool $memoize |
||
54 | * Memoize parameter. |
||
55 | */ |
||
56 | 4 | public static function addDynamicMethod($name, \Closure $func, $memoize = false) |
|
64 | |||
65 | /** |
||
66 | * Check if a dynamic property exists. |
||
67 | * |
||
68 | * @param string $name |
||
69 | * The property name. |
||
70 | * @return bool |
||
71 | * True if the property exists, false otherwise. |
||
72 | */ |
||
73 | 8 | public static function hasDynamicProperty($name) |
|
77 | |||
78 | /** |
||
79 | * Check if a dynamic method exists. |
||
80 | * |
||
81 | * @param string $name |
||
82 | * The property name. |
||
83 | * @return bool |
||
84 | * True if the property exists, false otherwise. |
||
85 | */ |
||
86 | 6 | public static function hasDynamicMethod($name) |
|
90 | |||
91 | /** |
||
92 | * Get a dynamic property. |
||
93 | * |
||
94 | * @param $name |
||
95 | * The property name. |
||
96 | * @return mixed|null |
||
97 | * The property value if it exists, null otherwise. |
||
98 | */ |
||
99 | 4 | public static function getDynamicProperty($name) |
|
107 | |||
108 | /** |
||
109 | * Get a dynamic method. |
||
110 | * |
||
111 | * @param $name |
||
112 | * The method name. |
||
113 | * @return mixed|null |
||
114 | * The method if it exists, null otherwise. |
||
115 | */ |
||
116 | 2 | public static function getDynamicMethod($name) |
|
121 | |||
122 | /** |
||
123 | * Clear dynamic properties. |
||
124 | */ |
||
125 | 1 | public static function clearDynamicProperties() |
|
129 | |||
130 | /** |
||
131 | * Clear dynamic methods. |
||
132 | */ |
||
133 | 1 | public static function clearDynamicMethods() |
|
137 | |||
138 | /** |
||
139 | * Remove a dynamic property. |
||
140 | * |
||
141 | * @param string $name |
||
142 | * The property name. |
||
143 | */ |
||
144 | 1 | public static function removeDynamicProperty($name) |
|
148 | |||
149 | /** |
||
150 | * Remove a dynamic method. |
||
151 | * |
||
152 | * @param string $name |
||
153 | * The method name. |
||
154 | */ |
||
155 | 1 | public static function removeDynamicMethod($name) |
|
159 | |||
160 | /** |
||
161 | * {inheritdoc} |
||
162 | */ |
||
163 | 3 | public function __call($method, array $parameters = array()) |
|
189 | |||
190 | /** |
||
191 | * {inheritdoc} |
||
192 | */ |
||
193 | 3 | public function __set($property, $value) |
|
199 | |||
200 | /** |
||
201 | * {inheritdoc} |
||
202 | */ |
||
203 | 4 | public function __get($property) |
|
232 | } |
||
233 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: