1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @filesource trait.php |
4
|
|
|
* @created 21.02.2016 |
5
|
|
|
* @author Smiley <[email protected]> |
6
|
|
|
* @copyright 2016 Smiley |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Example; |
11
|
|
|
|
12
|
|
|
use chillerlan\Database\DBOptions; |
13
|
|
|
use chillerlan\Database\Drivers\MySQLi\MySQLiDriver; |
14
|
|
|
use chillerlan\Database\Traits\DatabaseTrait; |
15
|
|
|
use Dotenv\Dotenv; |
16
|
|
|
use stdClass; |
17
|
|
|
|
18
|
|
|
require_once '../vendor/autoload.php'; |
19
|
|
|
|
20
|
|
|
date_default_timezone_set('UTC'); |
21
|
|
|
mb_internal_encoding('UTF-8'); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class MyClass |
25
|
|
|
*/ |
26
|
|
|
class MyClass{ |
27
|
|
|
use DatabaseTrait; |
28
|
|
|
|
29
|
|
|
protected $DBDriverInterface; |
30
|
|
|
protected $options; |
31
|
|
|
|
32
|
|
|
public function __construct(stdClass $class_options){ |
33
|
|
|
$this->options = $class_options; |
34
|
|
|
|
35
|
|
|
$this->DBDriverInterface = $this->dbconnect($this->options->dbdriver, $this->options->dbconfig); |
36
|
|
|
|
37
|
|
|
var_dump($this->DBDriverInterface->getClientInfo()); |
38
|
|
|
var_dump($this->DBDriverInterface->getServerInfo()); |
39
|
|
|
var_dump($this->DBDriverInterface->raw('SELECT 1 + 1')); |
40
|
|
|
|
41
|
|
|
// Inherited from the trait, holds the resource object of the last DatabaseTrait::dbconnect() call. |
42
|
|
|
var_dump($this->db); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function __destruct(){ |
46
|
|
|
$this->DBDriverInterface->disconnect(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* ogogog! |
53
|
|
|
*/ |
54
|
|
|
|
55
|
|
|
(new Dotenv(__DIR__.'/../config'))->load(); |
56
|
|
|
|
57
|
|
|
// let's assume we get the configuration from somewhere outside |
58
|
|
|
$class_options = new stdClass; |
59
|
|
|
$class_options->dbdriver = MySQLiDriver::class; |
60
|
|
|
$class_options->dbconfig = new DBOptions([ |
61
|
|
|
'host' => getenv('DB_MYSQLI_HOST'), |
62
|
|
|
'port' => getenv('DB_MYSQLI_PORT'), |
63
|
|
|
'database' => getenv('DB_MYSQLI_DATABASE'), |
64
|
|
|
'username' => getenv('DB_MYSQLI_USERNAME'), |
65
|
|
|
'password' => getenv('DB_MYSQLI_PASSWORD'), |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
new MyClass($class_options); |
69
|
|
|
|