|
1
|
|
|
<?php |
|
2
|
|
|
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; |
|
3
|
|
|
|
|
4
|
|
|
class Setting extends Base { |
|
5
|
|
|
protected $table = 'settings'; |
|
6
|
|
|
private $cache = array(); |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Fetch all values available and cache them in this class |
|
10
|
|
|
* That way we don't fetch them from DB for each call |
|
11
|
|
|
*/ |
|
12
|
|
|
public function createCache() { |
|
13
|
|
|
if ($aSettings = $this->getAllAssoc()) { |
|
14
|
|
|
foreach ($aSettings as $key => $aData) { |
|
15
|
|
|
$this->cache[$aData['name']] = $aData['value']; |
|
16
|
|
|
} |
|
17
|
|
|
return true; |
|
18
|
|
|
} |
|
19
|
|
|
return false; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Flush our local cache, may be required for upgrades |
|
24
|
|
|
* or other places where we need live data |
|
25
|
|
|
**/ |
|
26
|
|
|
public function flushCache() { |
|
27
|
|
|
$this->cache = array(); |
|
28
|
|
|
return true; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Fetch a value from our table |
|
33
|
|
|
* @param name string Setting name |
|
34
|
|
|
* @return value string Value |
|
35
|
|
|
**/ |
|
36
|
|
|
public function getValue($name, $default="") { |
|
37
|
|
|
// Try our class cache first |
|
38
|
|
|
if (isset($this->cache[$name])) return $this->cache[$name]; |
|
39
|
|
|
$stmt = $this->mysqli->prepare("SELECT value FROM $this->table WHERE name = ? LIMIT 1"); |
|
40
|
|
|
if ($this->checkStmt($stmt) && $stmt->bind_param('s', $name) && $stmt->execute() && $result = $stmt->get_result()) { |
|
41
|
|
|
if ($result->num_rows > 0) { |
|
42
|
|
|
return $result->fetch_object()->value; |
|
43
|
|
|
} else { |
|
44
|
|
|
return $default; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
// Log error but return empty string |
|
48
|
|
|
$this->sqlError(); |
|
49
|
|
|
return $default; |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Insert or update a setting |
|
54
|
|
|
* @param name string Name of the variable |
|
55
|
|
|
* @param value string Variable value |
|
56
|
|
|
* @return bool |
|
57
|
|
|
**/ |
|
58
|
|
View Code Duplication |
public function setValue($name, $value) { |
|
|
|
|
|
|
59
|
|
|
// Update local cache too |
|
60
|
|
|
$this->cache[$name] = $value; |
|
61
|
|
|
$stmt = $this->mysqli->prepare(" |
|
62
|
|
|
INSERT INTO $this->table (name, value) |
|
63
|
|
|
VALUES (?, ?) |
|
64
|
|
|
ON DUPLICATE KEY UPDATE value = ?"); |
|
65
|
|
|
if ($stmt && $stmt->bind_param('sss', $name, $value, $value) && $stmt->execute()) |
|
66
|
|
|
return true; |
|
67
|
|
|
return $this->sqlError(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$setting = new Setting($debug, $mysqli); |
|
|
|
|
|
|
72
|
|
|
$setting->setDebug($debug); |
|
73
|
|
|
$setting->setMysql($mysqli); |
|
74
|
|
|
$setting->setErrorCodes($aErrorCodes); |
|
75
|
|
|
// Fill our class cache with data so we don't have to run SQL queries all the time |
|
76
|
|
|
$setting->createCache(); |
|
77
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.