1 | <?php |
||
17 | class DataBaseQuery |
||
18 | { |
||
19 | protected $tableName; |
||
20 | protected $splitTableField; |
||
21 | protected $formatTableValues; |
||
22 | protected $dbConnection; |
||
23 | |||
24 | /** |
||
25 | * This is a constructor; a default method that will be called automatically during class |
||
26 | * instantiation. |
||
27 | */ |
||
28 | public function __construct($dbConn = null) |
||
36 | 72 | ||
37 | 51 | /** |
|
38 | 34 | * This method create or insert new users to the table. |
|
39 | 72 | * |
|
40 | * @param $associativeArray |
||
41 | 72 | * @param $tableName |
|
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | public function create($associativeArrayToCreate, $tableName) |
||
69 | |||
70 | 3 | /** |
|
71 | * This method read the data in the table name of the id being passed to it. |
||
72 | * |
||
73 | 3 | * @param $id |
|
74 | * @param $tableName |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | public static function read($id, $tableName, $dbConn = null) |
||
96 | 9 | ||
97 | 3 | /** |
|
98 | * This method delete the table name of the id being passed to it. |
||
99 | * |
||
100 | 6 | * @param $update Params |
|
101 | * @param $associativeArrayToUpdate |
||
102 | * @param $tableName |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function update($updateParams, $associativeArrayToUpdate, $tableName) |
||
132 | |||
133 | 3 | /** |
|
134 | * This method delete the table name of the id passed to it. |
||
135 | * |
||
136 | 3 | * @param $id |
|
137 | * @param $tableName |
||
138 | * |
||
139 | * @return bool |
||
140 | */ |
||
141 | public static function delete($id, $tableName, $dbConn = null) |
||
161 | |||
162 | 6 | /** |
|
163 | 6 | * This method returns a string form an array by making us of the implode function. |
|
164 | * |
||
165 | 6 | * @param $tableField |
|
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function splitTableField($tableField) |
||
175 | 12 | ||
176 | /** |
||
177 | 12 | * This method returns a string formed fron an array, It format the array. |
|
178 | * |
||
179 | 12 | * @param $tableValues |
|
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | public function formatTableValues($tableValues) |
||
195 | 2 | ||
196 | /** |
||
197 | 3 | * This method returns a string formed from an array. |
|
198 | * |
||
199 | 3 | * @param $array |
|
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | public function updateArraySql($array) |
||
215 | 4 | ||
216 | /** |
||
217 | 6 | * This method returns column fields of a particular table. |
|
218 | * |
||
219 | 6 | * @param $table |
|
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | public function getColumnNames($table) |
||
239 | } |
||
240 |
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.