Complex classes like MySQL often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MySQL, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | class MySQL extends \MySQLi |
||
5 | { |
||
6 | private $dbConfig = array(); |
||
7 | |||
8 | private $connected = false; |
||
9 | |||
10 | private $lastError = ''; |
||
11 | |||
12 | private $tables = []; |
||
13 | |||
14 | public function __construct( |
||
31 | |||
32 | public function connect( |
||
68 | |||
69 | public function error($str = '', $fatal = false) |
||
81 | |||
82 | /** |
||
83 | * Performs a generic query |
||
84 | * |
||
85 | * @param string $sql |
||
86 | * @return MySQL\Result|false |
||
87 | */ |
||
88 | public function query($sql, $resultMode = MYSQLI_STORE_RESULT) |
||
111 | |||
112 | /** |
||
113 | * Performs a INSERT statement |
||
114 | * |
||
115 | * @param string $tableName |
||
116 | * @param array $fields |
||
117 | * @return int Returns the ID of the inserted row, or false on error |
||
118 | */ |
||
119 | public function insert($tableName, $fields) |
||
139 | |||
140 | public function escape($str, $quoted = false) |
||
148 | |||
149 | private function parseWhere($where) |
||
175 | |||
176 | /** |
||
177 | * Performs an UPDATE statement |
||
178 | * |
||
179 | * @param string $tableName The name of the table |
||
180 | * @param array $fields The fields to update |
||
181 | * @param mixed $where Accepts array, string and integer |
||
182 | * @param int $limit (Optional) The limit of rows to update |
||
183 | * @return int Returns the number of affected rows, or false on error |
||
184 | */ |
||
185 | public function update($tableName, $fields, $where, $limit = null) |
||
217 | |||
218 | /** |
||
219 | * Performs a DELETE statement |
||
220 | * |
||
221 | * @param string $tableName The name of the table |
||
222 | * @param string $where The where |
||
223 | * @param int $limit (Optional) The limit |
||
224 | * @return int Returns the number of affected rows, or false on error |
||
225 | */ |
||
226 | public function delete($tableName, $where, $limit = null) |
||
250 | |||
251 | /** |
||
252 | * Performs a SELECT statement |
||
253 | * |
||
254 | * @param string $tableName The name of the table |
||
255 | * @param mixed $fields (Optional) The fields you want to obtain in the result. Accepts array or string |
||
256 | * @param mixed $where (Optional) The where. Accepts array, string or intenger |
||
257 | * @param string $orderBy (Optional) The order by |
||
258 | * @param int $limit (Optional) The limit |
||
259 | * @return MySQL_Result |
||
260 | */ |
||
261 | public function select($tableName, $fields = null, $where = null, $orderBy = null, $limit = null) |
||
297 | |||
298 | public function table($tableName, $tableArgs = []) |
||
306 | |||
307 | /** |
||
308 | * Close the connection when instance is destroyed. |
||
309 | */ |
||
310 | public function __destruct() |
||
318 | } |
||
319 |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.