1 | <?php |
||
7 | class Main { |
||
8 | |||
9 | /** |
||
10 | * Http status code |
||
11 | * @var integer |
||
12 | */ |
||
13 | protected $statusCode = 200; |
||
14 | |||
15 | /** |
||
16 | * Status text |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $statusText = 'success'; |
||
20 | |||
21 | /** |
||
22 | * Error code, message and text-key |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $error; |
||
26 | |||
27 | /** |
||
28 | * Error code |
||
29 | * @var integer |
||
30 | */ |
||
31 | protected $errorCode; |
||
32 | |||
33 | /** |
||
34 | * Haeders |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $headers = []; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $lang; |
||
43 | |||
44 | /** |
||
45 | * @var arry |
||
46 | */ |
||
47 | protected $config; |
||
48 | |||
49 | /** |
||
50 | * @author Shahrokh Niakan <[email protected]> |
||
51 | * @since Sep 24, 2016 |
||
52 | */ |
||
53 | public function __construct() { |
||
60 | |||
61 | /** |
||
62 | * Getter for $statusCode |
||
63 | * @author Shima Payro <[email protected]> |
||
64 | * @since May 2, 2016 9:46:27 AM |
||
65 | * @uses |
||
66 | * @see |
||
67 | */ |
||
68 | public function getStatusCode() { |
||
73 | |||
74 | /** |
||
75 | * Setter for $statusCode |
||
76 | * @param integer $statusCode |
||
77 | * @return App\Htpp\Responds\Respond |
||
78 | * @author Shima Payro <[email protected]> |
||
79 | * @since May 2, 2016 9:47:04 AM |
||
80 | * @uses |
||
81 | * @see |
||
82 | */ |
||
83 | public function setStatusCode( $statusCode ) { |
||
90 | |||
91 | /** |
||
92 | * Getter for $statusText |
||
93 | * @author Shima Payro <[email protected]> |
||
94 | * @since May 2, 2016 9:47:36 AM |
||
95 | * @uses |
||
96 | * @see |
||
97 | */ |
||
98 | public function getStatusText() { |
||
103 | |||
104 | /** |
||
105 | * Setter for $statusText |
||
106 | * @param String $statusText |
||
107 | * @return App\HtppApp\Htpp\Responds\Respond |
||
108 | * @author Shima Payro <[email protected]> |
||
109 | * @since May 2, 2016 9:48:23 AM |
||
110 | * @uses |
||
111 | * @see |
||
112 | */ |
||
113 | public function setStatusText( $statusText ) { |
||
120 | |||
121 | /** |
||
122 | * Response |
||
123 | * @param json $data |
||
124 | * @return jsom |
||
125 | * @author Shima Payro <[email protected]> |
||
126 | * @since May 2, 2016 9:48:45 AM |
||
127 | * @uses |
||
128 | * @see |
||
129 | */ |
||
130 | public function respond( $data ) { |
||
141 | |||
142 | /** |
||
143 | * Response which conteins just a message |
||
144 | * @param string $message |
||
145 | * @author Shima Payro <[email protected]> |
||
146 | * @since May 2, 2016 9:49:21 AM |
||
147 | * @return json |
||
148 | * @uses |
||
149 | * @see |
||
150 | */ |
||
151 | public function respondWithMessage( $message ) { |
||
174 | |||
175 | /** |
||
176 | * Set error code in our result |
||
177 | * @author Mehdi Hosseini <[email protected]> |
||
178 | * @since August 24, 2016 |
||
179 | * @param $errorCode integer |
||
180 | * @return instance |
||
181 | */ |
||
182 | public function setErrorCode( $errorCode ) { |
||
191 | |||
192 | /** |
||
193 | * Return Error code |
||
194 | * @author Mehdi Hosseini <[email protected]> |
||
195 | * @since August 24, 2016 |
||
196 | * @return integer |
||
197 | */ |
||
198 | public function getErrorCode() { |
||
203 | |||
204 | /** |
||
205 | * Get error message |
||
206 | * @author Mehdi Hosseini <[email protected]> |
||
207 | * @since August 24, 2016 |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getErrorMessage() { |
||
215 | |||
216 | /** |
||
217 | * Get headers |
||
218 | * @author Shima Payro <[email protected]> |
||
219 | * @since Sep 13, 2016 |
||
220 | * @return array |
||
221 | */ |
||
222 | public function getHeaders() { |
||
227 | |||
228 | /** |
||
229 | * Set headers |
||
230 | * @author Shima Payro <[email protected]> |
||
231 | * @since Sep 13, 2016 |
||
232 | * @return array |
||
233 | */ |
||
234 | public function setHeaders( $headers = [] ) { |
||
241 | |||
242 | /** |
||
243 | * Response which contains status and data |
||
244 | * @param json $data |
||
245 | * @author Shima Payro <[email protected]> |
||
246 | * @since May 2, 2016 9:50:19 AM |
||
247 | * @return json |
||
248 | * @uses |
||
249 | * @see |
||
250 | */ |
||
251 | public function respondWithResult( $data = NULL ) { |
||
270 | |||
271 | } |
||
272 |
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.