1 | <?php |
||
28 | class Database |
||
29 | { |
||
30 | use User; |
||
31 | |||
32 | /** @} */ |
||
33 | |||
34 | /** PDO connection to the database. */ |
||
35 | public $pdo; |
||
36 | |||
37 | /** \brief Table contaning bot users data in the SQL database. */ |
||
38 | public $user_table = 'User'; |
||
39 | |||
40 | /** |
||
41 | * \addtogroup Database |
||
42 | * @{ |
||
43 | */ |
||
44 | |||
45 | /** |
||
46 | * @internal |
||
47 | * \brief Create a Database handler object. |
||
48 | * @param BasicBot $bot Reference to the bot that use this object. |
||
49 | */ |
||
50 | public function __construct(BasicBot &$bot) |
||
54 | |||
55 | /** |
||
56 | * \brief Open a database connection using PDO. |
||
57 | * \details Provides a simple way to initialize a database connection |
||
58 | * and create a PDO instance. |
||
59 | * @param array $params Parameters for initialize connection. |
||
60 | * Index required: |
||
61 | * - <code>username</code> |
||
62 | * - <code>password</code> (can be an empty string) |
||
63 | * Optional index: |
||
64 | * - <code>dbname</code> |
||
65 | * - <code>adapter</code> <b>Default</b>: <code>mysql</code> |
||
66 | * - <code>host</code> <b>Default</b>: <code>localhost</code> |
||
67 | * - <code>options</code> (<i>Array of options passed when creating PDO object</i>) |
||
68 | * @return bool True when the connection is succefully created. |
||
69 | */ |
||
70 | public function connect(array $params) : bool |
||
86 | |||
87 | /** |
||
88 | * @internal |
||
89 | * \brief Add default connection value to parameter passed to PDO. |
||
90 | * @param array $params Parameter for PDO connection. |
||
91 | * @return array Parameter with defaults value. |
||
92 | */ |
||
93 | protected function addDefaultValue(array $params) : array |
||
98 | |||
99 | /** |
||
100 | * @internal |
||
101 | * \brief Returns a string that can passed to PDO as DNS parameter in order to open connection. |
||
102 | * @param array $params Array containing parameter of the connection |
||
103 | * @return string Parameters contained in array $params sanitized in a string that can be passed as DNS param of PDO object creation. |
||
104 | */ |
||
105 | protected function getDns($params) : string |
||
127 | |||
128 | /** |
||
129 | * @internal |
||
130 | * \brief Sanitize name of the user table depending on database used. |
||
131 | */ |
||
132 | protected function sanitizeUserTable() |
||
148 | |||
149 | /** @} */ |
||
150 | } |
||
151 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: