1 | <?php |
||
30 | class PDOConnection extends PDO implements Connection, ServerInfoAwareConnection |
||
31 | { |
||
32 | /** |
||
33 | * @var LastInsertId |
||
34 | */ |
||
35 | private $lastInsertId; |
||
36 | |||
37 | /** |
||
38 | * @param string $dsn |
||
39 | * @param string|null $user |
||
40 | * @param string|null $password |
||
41 | * @param array|null $options |
||
42 | * |
||
43 | * @throws PDOException in case of an error. |
||
44 | */ |
||
45 | 80 | public function __construct($dsn, $user = null, $password = null, array $options = null) |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 176 | public function exec($statement) |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function getServerVersion() |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 142 | public function prepare($prepareString, $driverOptions = []) |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 172 | public function query() |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 4 | public function quote($input, $type = \PDO::PARAM_STR) |
|
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 33 | public function lastInsertId($name = null) |
|
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | 1 | public function requiresQueryForServerVersion() |
|
169 | |||
170 | /** |
||
171 | * Tracks the last insert ID at the current state. |
||
172 | * |
||
173 | * If this PDO driver is not able to fetch the last insert ID for identity columns |
||
174 | * without influencing connection state or transaction state, this is a noop method. |
||
175 | * |
||
176 | * @internal this method is only supposed to be used in DBAL internals |
||
177 | * |
||
178 | * @throws \PDOException |
||
179 | */ |
||
180 | 227 | public function trackLastInsertId() : void |
|
203 | |||
204 | /** |
||
205 | * Fetches the last insert ID generated by this connection. |
||
206 | * |
||
207 | * This method queries the database connection for the last insert ID. |
||
208 | * |
||
209 | * @param string|null $sequenceName The name of the sequence to retrieve the last insert ID from, |
||
210 | * if not given the overall last insert ID is returned. |
||
211 | * |
||
212 | * @return string The last insert ID or '0' in case the last insert ID generated on this connection is unknown. |
||
213 | * |
||
214 | * @throws \PDOException in case of an error. |
||
215 | */ |
||
216 | 227 | protected function fetchLastInsertId(?string $sequenceName) : string |
|
220 | } |
||
221 |
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.