1 | <?php |
||
18 | class Database { |
||
|
|||
19 | |||
20 | /** |
||
21 | * @access public |
||
22 | * @var PDO PDO Object |
||
23 | */ |
||
24 | private $connection = null; |
||
25 | |||
26 | /** |
||
27 | * @access public |
||
28 | * @var PDOStatement PDOStatement Object |
||
29 | */ |
||
30 | private $statement = null; |
||
31 | |||
32 | /** |
||
33 | * @access public |
||
34 | * @static static |
||
35 | * @var Database Database Object used to implement the Singleton pattern |
||
36 | */ |
||
37 | private static $database = null; |
||
38 | |||
39 | /** |
||
40 | * This is the constructor for Database Object. |
||
41 | * |
||
42 | * @access private |
||
43 | */ |
||
44 | private function __construct() { |
||
55 | |||
56 | /** |
||
57 | * This method for instantiating database object using the Singleton Design pattern. |
||
58 | * |
||
59 | * @access public |
||
60 | * @static static method |
||
61 | * @return Database Instantiate(if not already instantiated) |
||
62 | * |
||
63 | */ |
||
64 | public static function openConnection(){ |
||
70 | |||
71 | /** |
||
72 | * Prepares a SQL query for execution and assign a statement object to $this->statement |
||
73 | * |
||
74 | * @access public |
||
75 | * @param string $query |
||
76 | * |
||
77 | */ |
||
78 | public function prepare($query) { |
||
81 | |||
82 | /** |
||
83 | * Binds a value to a parameter. |
||
84 | * |
||
85 | * A better practice to explicitly define data types in parameter declarations, |
||
86 | * So, instead of defining the data type parameter every time, |
||
87 | * Just pass the value, and getPDOType() will take care of it's data type |
||
88 | * |
||
89 | * This is the same for bindParam() |
||
90 | * |
||
91 | * @access public |
||
92 | * @param string $param |
||
93 | * @param mixed $value |
||
94 | * |
||
95 | */ |
||
96 | public function bindValue($param, $value) { |
||
100 | |||
101 | /** |
||
102 | * Binds variable by reference to a parameter. |
||
103 | * |
||
104 | * @access public |
||
105 | * @param string $param |
||
106 | * @param mixed $var |
||
107 | * |
||
108 | */ |
||
109 | public function bindParam($param, &$var) { |
||
113 | |||
114 | /** |
||
115 | * Executes a prepared statement |
||
116 | * |
||
117 | * @access public |
||
118 | * @param array Array of values to be bound in SQL query, All values are treated as PDO::PARAM_STR. |
||
119 | * @return boolean Returns TRUE on success or FALSE on failure. |
||
120 | * |
||
121 | */ |
||
122 | public function execute($arr = null){ |
||
126 | |||
127 | /** |
||
128 | * To fetch only a single column in form of 0-indexed array. |
||
129 | * |
||
130 | * @access public |
||
131 | * @return array |
||
132 | */ |
||
133 | public function fetchColumn() { |
||
136 | |||
137 | /** |
||
138 | * To fetch the result data in form of [0-indexed][key][value] array. |
||
139 | * |
||
140 | * @access public |
||
141 | * @return array empty array if no data returned |
||
142 | */ |
||
143 | public function fetchAllAssociative() { |
||
146 | |||
147 | /** |
||
148 | * To fetch Only the next row from the result data in form of [key][value] array. |
||
149 | * |
||
150 | * @access public |
||
151 | * @return array|bool false on if no data returned |
||
152 | */ |
||
153 | public function fetchAssociative() { |
||
156 | |||
157 | /** |
||
158 | * To fetch All the data in form of [0-indexed][an anonymous object with property names that correspond to the column names] array. |
||
159 | * |
||
160 | * @access public |
||
161 | * @return array |
||
162 | */ |
||
163 | public function fetchAllObject() { |
||
166 | |||
167 | /** |
||
168 | * To fetch Only the next row from the result data in form of an anonymous object with property names that correspond to the column names. |
||
169 | * |
||
170 | * @access public |
||
171 | * @return object |
||
172 | */ |
||
173 | public function fetchObject() { |
||
176 | |||
177 | /** |
||
178 | * To fetch All data in form of an array indexed by both column name and 0-indexed column |
||
179 | * |
||
180 | * @access public |
||
181 | * @return array |
||
182 | */ |
||
183 | public function fetchAllBoth() { |
||
186 | |||
187 | /** |
||
188 | * To fetch Only the next row from the result data in form of an array indexed by both column name and 0-indexed column |
||
189 | * |
||
190 | * @access public |
||
191 | * @return array |
||
192 | */ |
||
193 | public function fetchBoth() { |
||
196 | |||
197 | /** |
||
198 | * Returns the ID of the last inserted row or sequence value |
||
199 | * "This method may not return a meaningful or consistent result across different PDO drivers" |
||
200 | * |
||
201 | * @access public |
||
202 | * @return integer The ID of the last inserted row of Auto-incremented primary key. |
||
203 | * @see http://php.net/manual/en/pdo.lastinsertid.php |
||
204 | * |
||
205 | */ |
||
206 | public function lastInsertedId() { |
||
209 | |||
210 | /** |
||
211 | * Start a transaction |
||
212 | * |
||
213 | * @access public |
||
214 | * @see http://php.net/manual/en/pdo.begintransaction.php |
||
215 | * @see http://stackoverflow.com/questions/10155946/php-pdo-transactions |
||
216 | */ |
||
217 | public function beginTransaction() { |
||
220 | |||
221 | /** |
||
222 | * Commit a transaction. This method will be called after beginTransaction() |
||
223 | * |
||
224 | * It will return the database connection to autocommit mode until the next call to PDO::beginTransaction() starts a new transaction. |
||
225 | * |
||
226 | * @access public |
||
227 | * @see http://php.net/manual/en/pdo.commit.php |
||
228 | */ |
||
229 | public function commit() { |
||
232 | |||
233 | /** |
||
234 | * Rollback a transaction. This method will be called after beginTransaction() |
||
235 | * A PDOException will be thrown if no transaction is active. |
||
236 | * |
||
237 | * It will return the database connection to autocommit mode until the next call to PDO::beginTransaction() starts a new transaction. |
||
238 | * |
||
239 | * @access public |
||
240 | * @see http://php.net/manual/en/pdo.rollback.php |
||
241 | */ |
||
242 | public function rollBack() { |
||
245 | |||
246 | /** |
||
247 | * Returns the number of rows affected by the last SQL statement |
||
248 | * "If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement" |
||
249 | * |
||
250 | * @access public |
||
251 | * @see http://php.net/manual/en/pdostatement.rowcount.php |
||
252 | */ |
||
253 | public function countRows() { |
||
256 | |||
257 | /** |
||
258 | * Counts the number of rows in a specific table |
||
259 | * |
||
260 | * @access public |
||
261 | * @param string $table |
||
262 | * @return integer |
||
263 | * |
||
264 | */ |
||
265 | public function countAll($table){ |
||
270 | |||
271 | /** |
||
272 | * Select all rows from a table |
||
273 | * |
||
274 | * @access public |
||
275 | * @param string $table |
||
276 | * |
||
277 | */ |
||
278 | public function getAll($table){ |
||
282 | |||
283 | /** |
||
284 | * Select a row from a table provided by id(primary key) |
||
285 | * |
||
286 | * @access public |
||
287 | * @param string $table |
||
288 | * @param mixed $id |
||
289 | * |
||
290 | */ |
||
291 | public function getById($table, $id){ |
||
296 | |||
297 | /** |
||
298 | * Select a row from a table provided by email |
||
299 | * |
||
300 | * @access public |
||
301 | * @param string $table |
||
302 | * @param string $email |
||
303 | * |
||
304 | */ |
||
305 | public function getByEmail($table, $email){ |
||
310 | |||
311 | /** |
||
312 | * Select all rows from a table provided by user id |
||
313 | * |
||
314 | * @access public |
||
315 | * @param string $table |
||
316 | * @param integer $userId |
||
317 | * |
||
318 | */ |
||
319 | public function getByUserId($table, $userId){ |
||
324 | |||
325 | /** |
||
326 | * Select all rows from a table provided by user email |
||
327 | * |
||
328 | * @access public |
||
329 | * @param string $table |
||
330 | * @param string $user_email |
||
331 | * |
||
332 | */ |
||
333 | public function getByUserEmail($table, $user_email){ |
||
338 | |||
339 | /** |
||
340 | * Delete all rows from a table |
||
341 | * |
||
342 | * @access public |
||
343 | * @param string $table |
||
344 | * |
||
345 | */ |
||
346 | public function deleteAll($table){ |
||
350 | |||
351 | /** |
||
352 | * Delete all data from a table provided by id(primary key) |
||
353 | * |
||
354 | * @access public |
||
355 | * @param string $table |
||
356 | * @param mixed $id |
||
357 | * |
||
358 | */ |
||
359 | public function deleteById($table, $id){ |
||
364 | |||
365 | /** |
||
366 | * Determine the PDOType of a passed value. |
||
367 | * This is done by determining the data type of the passed value. |
||
368 | * |
||
369 | * @access public |
||
370 | * @param mixed $value |
||
371 | * @return integer PDO Constants |
||
372 | * |
||
373 | */ |
||
374 | private static function getPDOType($value){ |
||
386 | |||
387 | /** |
||
388 | * Closing the connection. |
||
389 | * |
||
390 | * It's not necessary to close the connection, however it's a good practice. |
||
391 | * "If you don't do this explicitly, PHP will automatically close the connection when your script ends." |
||
392 | * |
||
393 | * This will be used at the end "footer.php" |
||
394 | * |
||
395 | * @static static method |
||
396 | * @access public |
||
397 | * @see http://php.net/manual/en/pdo.connections.php |
||
398 | */ |
||
399 | public static function closeConnection() { |
||
406 | |||
407 | } |
||
408 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.