MySQLi in een class gebruiken.
Hallo
Is er een mogelijkheid om een MySQLi object te gebruiken in een class? Het volgende heb ik. Maar is niet goed.
Maar wat is wel de juiste methode?
Met vriendelijke groet,
Kevin van der Burgt
Is er een mogelijkheid om een MySQLi object te gebruiken in een class? Het volgende heb ik. Maar is niet goed.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
$mysqli = new MySQLi('HOST', 'USER', 'PASS', 'DB');
$TEST = new clsConfig($mysqli);
if($TEST->LoadConfig()){ echo "1"; } else { echo "2"; }
class clsConfig{
private $objMySQLi = NULL;
public function __construct($obj){
if(!is_object($obj)) return false;
self::$objMySQLi = $obj;
return true;
}
public function LoadConfig(){
$qSelect = "SELECT skey, sval FROM setting ORDER BY ASC";
$res = self::$objMySQLi->query($qSelect);
if(!$res) return false;
return true;
}
}
?>
$mysqli = new MySQLi('HOST', 'USER', 'PASS', 'DB');
$TEST = new clsConfig($mysqli);
if($TEST->LoadConfig()){ echo "1"; } else { echo "2"; }
class clsConfig{
private $objMySQLi = NULL;
public function __construct($obj){
if(!is_object($obj)) return false;
self::$objMySQLi = $obj;
return true;
}
public function LoadConfig(){
$qSelect = "SELECT skey, sval FROM setting ORDER BY ASC";
$res = self::$objMySQLi->query($qSelect);
if(!$res) return false;
return true;
}
}
?>
Maar wat is wel de juiste methode?
Met vriendelijke groet,
Kevin van der Burgt
ik zat te denken op eerst de class en functies te starten en daarna de uitvoer
ik ben niet zo OOP kenner ik weet dus niet of er meer fout is
en een alleen een cijfer ga je tog niet echo'een
ik ben niet zo OOP kenner ik weet dus niet of er meer fout is
en een alleen een cijfer ga je tog niet echo'een
Gewijzigd op 09/01/2011 18:35:47 door Maarten PHP
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
class MySQLiException extends Exception {}
class Config
{
protected $mysqli; // =null niet nodig
public function __construct(MySQLi $mysqli) // Type hinting
{
$this->mysqli = $mysqli; // let op $this
}
public function loadConfig()
{
$query = 'query';
$res = $this->mysqli->query($query);
if(!$res) // Fouten net afhandelen
throw new MySQLiException($this->mysqli->error);
return $res->fetchAll()
}
}
$mysqli = new MySQLi('HOST', 'USER', 'PASS', 'DB');
$configObject = new Config($mysqli);
$array = $configObject->loadConfig();
?>
class MySQLiException extends Exception {}
class Config
{
protected $mysqli; // =null niet nodig
public function __construct(MySQLi $mysqli) // Type hinting
{
$this->mysqli = $mysqli; // let op $this
}
public function loadConfig()
{
$query = 'query';
$res = $this->mysqli->query($query);
if(!$res) // Fouten net afhandelen
throw new MySQLiException($this->mysqli->error);
return $res->fetchAll()
}
}
$mysqli = new MySQLi('HOST', 'USER', 'PASS', 'DB');
$configObject = new Config($mysqli);
$array = $configObject->loadConfig();
?>