1 | <?php |
||
4 | class HttpResponse |
||
5 | { |
||
6 | private $code; |
||
7 | private $raw_body; |
||
8 | private $body; |
||
9 | private $headers; |
||
10 | |||
11 | /** |
||
12 | * @param int $code response code of the cURL request |
||
13 | * @param string $raw_body the raw body of the cURL response |
||
14 | * @param string $headers raw header string from cURL response |
||
15 | */ |
||
16 | public function __construct($code, $raw_body, $headers) |
||
27 | |||
28 | /** |
||
29 | * Return a property of the response if it exists. |
||
30 | * Possibilities include: code, raw_body, headers, body (if the response is json-decodable) |
||
31 | * @return mixed |
||
32 | */ |
||
33 | public function __get($property) |
||
39 | |||
40 | /** |
||
41 | * Set the properties of this object |
||
42 | * @param string $property the property name |
||
43 | * @param mixed $value the property value |
||
44 | */ |
||
45 | public function __set($property, $value) |
||
52 | |||
53 | /** |
||
54 | * Retrieve the cURL response headers from the |
||
55 | * header string and convert it into an array |
||
56 | * @param string $headers header string from cURL response |
||
57 | * @return array |
||
58 | */ |
||
59 | private function get_headers_from_curl_response($headers) |
||
71 | } |
||
72 |
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.