1 | <?php |
||
11 | class Database { |
||
12 | |||
13 | const POSTFIX_DUPLICATE_QUERY_VARIABLE = '_prefix'; |
||
14 | |||
15 | /** |
||
16 | * @var Cluster |
||
17 | */ |
||
18 | private $cluster; |
||
19 | |||
20 | /** |
||
21 | * @var Connection |
||
22 | */ |
||
23 | private $connection; |
||
24 | |||
25 | /** |
||
26 | * Connection options |
||
27 | * @var array |
||
28 | */ |
||
29 | private $options = [ |
||
30 | 'CQL_VERSION' => '3.0.0' |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $keyspace; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $batchQuery = ''; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | private $batchQueryData = []; |
||
47 | |||
48 | /** |
||
49 | * @param array $nodes |
||
50 | * @param string $keyspace |
||
51 | * @param array $options |
||
52 | */ |
||
53 | public function __construct(array $nodes, $keyspace = '', array $options = []) { |
||
59 | |||
60 | /** |
||
61 | * Connect to database |
||
62 | * @throws Exception\ConnectionException |
||
63 | * @throws Exception\CassandraException |
||
64 | * @return bool |
||
65 | */ |
||
66 | public function connect() { |
||
92 | |||
93 | /** |
||
94 | * Disconnect to database |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function disconnect() { |
||
101 | |||
102 | /** |
||
103 | * Start transaction |
||
104 | */ |
||
105 | public function beginBatch() { |
||
111 | |||
112 | /** |
||
113 | * Start counter transaction |
||
114 | */ |
||
115 | public function beginCounterBatch() { |
||
121 | |||
122 | /** |
||
123 | * Start unlogged transaction |
||
124 | */ |
||
125 | public function beginUnloggedBatch() { |
||
131 | |||
132 | /** |
||
133 | * Exec transaction |
||
134 | */ |
||
135 | public function applyBatch($consistency = ConsistencyEnum::CONSISTENCY_QUORUM) { |
||
144 | |||
145 | /** |
||
146 | * @param string $cql |
||
147 | * @param array $values |
||
148 | */ |
||
149 | private function appendQueryToStack($cql, array $values) { |
||
168 | |||
169 | /** |
||
170 | * Send query into database |
||
171 | * @param string $cql |
||
172 | * @param array $values |
||
173 | * @param int $consistency |
||
174 | * @throws Exception\QueryException |
||
175 | * @throws Exception\CassandraException |
||
176 | * @return array|null |
||
177 | */ |
||
178 | public function query($cql, array $values = [], $consistency = ConsistencyEnum::CONSISTENCY_QUORUM) { |
||
209 | |||
210 | /** |
||
211 | * @param string $keyspace |
||
212 | * @throws Exception\CassandraException |
||
213 | */ |
||
214 | public function setKeyspace($keyspace) { |
||
223 | } |
||
224 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.