1 | <?php |
||
22 | class DateTimePlus extends DateTime |
||
23 | { |
||
24 | |||
25 | /////////////////////////////////////////////////////////////////// |
||
26 | ///////////////////////// GETTERS AND SETTERS ///////////////////// |
||
27 | /////////////////////////////////////////////////////////////////// |
||
28 | /** |
||
29 | * Get a part of the Carbon object |
||
30 | * |
||
31 | * @param string $name |
||
32 | * |
||
33 | * @throws InvalidArgumentException |
||
34 | * |
||
35 | * @return string|int|\DateTimeZone |
||
36 | */ |
||
37 | public function __get($name) |
||
80 | |||
81 | /** |
||
82 | * Check if an attribute exists on the object |
||
83 | * |
||
84 | * @param string $name |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | public function __isset($name) |
||
97 | |||
98 | /** |
||
99 | * Set a part of the Date object |
||
100 | * |
||
101 | * @param string $name |
||
102 | * @param string|int|\DateTimeZone $value |
||
103 | * |
||
104 | * @throws InvalidArgumentException |
||
105 | */ |
||
106 | public function __set($name, $value) |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Set the instance's year |
||
142 | * @param int $value |
||
143 | * @return static |
||
144 | */ |
||
145 | public function year($value) |
||
150 | |||
151 | /** |
||
152 | * Set the instance's month |
||
153 | * @param int $value |
||
154 | * @return static |
||
155 | */ |
||
156 | public function month($value) |
||
161 | |||
162 | /** |
||
163 | * Set the instance's day |
||
164 | * |
||
165 | * @param int $value |
||
166 | * @return static |
||
167 | */ |
||
168 | public function day($value) |
||
173 | |||
174 | /** |
||
175 | * Set the instance's hour |
||
176 | * |
||
177 | * @param int $value |
||
178 | * @return static |
||
179 | */ |
||
180 | public function hour($value) |
||
185 | |||
186 | /** |
||
187 | * Set the instance's minute |
||
188 | * |
||
189 | * @param int $value |
||
190 | * @return static |
||
191 | */ |
||
192 | public function minute($value) |
||
197 | |||
198 | /** |
||
199 | * Set the instance's second |
||
200 | * |
||
201 | * @param int $value |
||
202 | * @return static |
||
203 | */ |
||
204 | public function second($value) |
||
209 | |||
210 | |||
211 | /** |
||
212 | * @inheritdoc |
||
213 | */ |
||
214 | public static function createFromFormat($format, $time, $timezone = null) |
||
228 | |||
229 | /** |
||
230 | * Modify date to this year |
||
231 | * |
||
232 | * @return static |
||
233 | */ |
||
234 | public function currentYear() |
||
238 | |||
239 | /** |
||
240 | * Create a DatePlus instance from a DateTime one. |
||
241 | * @param \DateTime $dt |
||
242 | * @return static |
||
243 | */ |
||
244 | public static function instance(\DateTime $dt) |
||
251 | } |
||
252 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$x
would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.