|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
jRequire("../Module/Module.php"); |
|
3
|
|
|
requireComponents("Adapters"); |
|
4
|
|
|
class Connection extends Module { |
|
|
|
|
|
|
5
|
|
|
public $database; |
|
6
|
|
|
public $info; |
|
7
|
|
|
public function __construct() { |
|
8
|
|
|
parent::__construct(); |
|
9
|
|
|
$args = func_get_args(); |
|
10
|
|
|
$count = func_num_args(); |
|
11
|
|
|
if (method_exists($this,$func='__construct'.$count)) |
|
12
|
|
|
call_user_func_array(array($this,$func),$args); |
|
13
|
|
|
} |
|
14
|
|
|
public function __construct0() { |
|
15
|
|
|
$this->database = null; |
|
16
|
|
|
} |
|
17
|
|
|
public function __construct4 ( $_srv, $_db, $_usr, $_pass ) { |
|
18
|
|
|
$this->setConnection($_srv, $_db, $_usr, $_pass, "pdo"); |
|
19
|
|
|
} |
|
20
|
|
|
public function __construct5 ( $_srv, $_db, $_usr, $_pass, $_type ) { |
|
21
|
|
|
$type = $this->getConnectionType($_type); |
|
22
|
|
|
$this->setConnection($_srv, $_db, $_usr, $_pass, $type); |
|
23
|
|
|
} |
|
24
|
|
|
protected function setConnection ( $_srv, $_db, $_usr, $_pass, $_type ) { |
|
25
|
|
|
switch ($_type) { |
|
26
|
|
|
case "mysqli": |
|
27
|
|
|
$this->database = new ConnectionMysqliAdapter($_srv, $_db, $_usr, $_pass); |
|
28
|
|
|
break; |
|
29
|
|
|
default: |
|
30
|
|
|
$this->database = new ConnectionPdoAdapter($_srv, $_db, $_usr, $_pass); |
|
31
|
|
|
break; |
|
32
|
|
|
} |
|
33
|
|
|
$this->setConnectionParameters( $_srv, $_db, $_usr, $_pass); |
|
34
|
|
|
} |
|
35
|
|
|
protected function getConnectionType( $_type ) { |
|
36
|
|
|
foreach ($_type as $key => $value) |
|
37
|
|
|
if($value) |
|
38
|
|
|
return $key; |
|
39
|
|
|
return "pdo"; |
|
40
|
|
|
} |
|
41
|
|
|
protected function setConnectionParameters( $_srv, $_db, $_usr, $_pass) { |
|
42
|
|
|
$this->info = []; |
|
43
|
|
|
$this->info["server"] = $_srv; |
|
44
|
|
|
$this->info["database"] = $_db; |
|
45
|
|
|
$this->info["user"] = $_usr; |
|
46
|
|
|
$this->info["password"] = $_pass; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
?> |
|
|
|
|
|
|
50
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.