Skip to content

Commit 61d655b

Browse files
committed
add printErrors property & refactor setError() & getErrorMessage() methods
1 parent 55cdd8d commit 61d655b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/QueryBuilder.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@ class QueryBuilder
1616
private const FETCH_ONE = 1;
1717
private const FETCH_ALL = 2;
1818
private const FETCH_COLUMN = 3;
19-
private $pdo = null;
19+
private PDO $pdo;
2020
private $query = null;
21-
private $sql = '';
22-
private $error = false;
23-
private $errorMessage = '';
24-
private $result = [];
25-
private $params = [];
26-
private $count = -1;
21+
private string $sql = '';
22+
private bool $error = false;
23+
private string $errorMessage = '';
24+
private bool $printErrors = false;
25+
private array $result = [];
26+
private array $params = [];
27+
private int $count = -1;
2728

2829
/**
2930
* @param PDO $pdo
3031
*/
31-
public function __construct(PDO $pdo)
32+
public function __construct(PDO $pdo, bool $printErrors = false)
3233
{
3334
$this->pdo = $pdo;
35+
$this->printErrors = $printErrors;
3436
}
3537

3638
/**
@@ -91,6 +93,10 @@ public function hasError(): bool
9193
*/
9294
public function getErrorMessage(): string
9395
{
96+
if ($this->printErrors && $this->error) {
97+
echo $this->errorMessage;
98+
}
99+
94100
return $this->errorMessage;
95101
}
96102

@@ -102,6 +108,10 @@ public function setError(string $message = ''): void
102108
{
103109
$this->error = !empty($message);
104110
$this->errorMessage = $message;
111+
112+
if ($this->printErrors && $this->error) {
113+
echo $this->errorMessage;
114+
}
105115
}
106116

107117
/**

0 commit comments

Comments
 (0)