Heya one2xs,
Opnieuw een classje wat jullie mogen hebben. Dit keer een config class. Dit heb ik gemaakt voor mijn ledensysteem, om makkelijk belangrijke waardes te kunnen opslaan en ophalen.
Codeblok (config.class.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
*
* Configuration Class
* Gets and sets all variables that might be used in the entire script. Keeps the configuration of
* current running script, and returns the configuration if needed.
*
* @author Rik S.
* @copyright Copyright (c) Just-Rik All rights reserved.
*/
class Config
{
/**
* Contains all existing configuration values
*
* @var array
*/
private static $arrConfig = array();
/**
* Adds multiple items to the existing configuration
*
* @param array $arrItems
*/
public static function addItems($arrItems)
{
if(!empty(self::$arrConfig))
{
foreach($arrItems AS $arrKey => $arrValue)
{
self::$arrConfig[$arrKey] = $arrValue;
}
}
else
{
self::$arrConfig = $arrItems;
}
}
/**
* Gets the value for the given key
*
* @param string $strItem
* @return $arrConfig[key]
*/
public static function getItem($strItem)
{
if(isset(self::$arrConfig[$strItem]))
{
return self::$arrConfig[$strItem];
}
else
{
return NULL;
}
}
/**
* Gets all values from the configuration
*
* @return $arrConfig
*/
public static function getAll()
{
return self::$arrConfig;
}
}
?>
Eerst maken we een config aan
Codeblok (config.php):
1
2
3
4
5
6
7
/**
* Configuration array. Contains all variables which need editting and which are needed within the system.
*/
Config::addItem(array(layout => default,
hash => sha512,
salt => Kb4n=fd89&^&d());
Vervolgens halen we de configwaardes die we nodig hebben op:
Codeblok (demo.php):
1
2
3
4
5
6
7
8
9
10
require "config.class.php";
require "config.php"
echo config::getItem(hash); // Geeft sha512 weer.
hash(config::getItem(hash), $pass . config::getItem(salt)); // Hashed $pass met onze config-hash + salt
?>
En dat was het dan weer.
Zoals altijd: Donaties zijn welkom, en laat de copyright graag staan. Ik ben overigens nu een boek over echt OOP programmeren aan het lezen, dus daarom geef ik een deel van mijn oude scripts vrij ^^
~ Rik
Opnieuw een classje wat jullie mogen hebben. Dit keer een config class. Dit heb ik gemaakt voor mijn ledensysteem, om makkelijk belangrijke waardes te kunnen opslaan en ophalen.
Codeblok (config.class.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
*
* Configuration Class
* Gets and sets all variables that might be used in the entire script. Keeps the configuration of
* current running script, and returns the configuration if needed.
*
* @author Rik S.
* @copyright Copyright (c) Just-Rik All rights reserved.
*/
class Config
{
/**
* Contains all existing configuration values
*
* @var array
*/
private static $arrConfig = array();
/**
* Adds multiple items to the existing configuration
*
* @param array $arrItems
*/
public static function addItems($arrItems)
{
if(!empty(self::$arrConfig))
{
foreach($arrItems AS $arrKey => $arrValue)
{
self::$arrConfig[$arrKey] = $arrValue;
}
}
else
{
self::$arrConfig = $arrItems;
}
}
/**
* Gets the value for the given key
*
* @param string $strItem
* @return $arrConfig[key]
*/
public static function getItem($strItem)
{
if(isset(self::$arrConfig[$strItem]))
{
return self::$arrConfig[$strItem];
}
else
{
return NULL;
}
}
/**
* Gets all values from the configuration
*
* @return $arrConfig
*/
public static function getAll()
{
return self::$arrConfig;
}
}
?>
Eerst maken we een config aan
Codeblok (config.php):
1
2
3
4
5
6
7
/**
* Configuration array. Contains all variables which need editting and which are needed within the system.
*/
Config::addItem(array(layout => default,
hash => sha512,
salt => Kb4n=fd89&^&d());
Vervolgens halen we de configwaardes die we nodig hebben op:
Codeblok (demo.php):
1
2
3
4
5
6
7
8
9
10
require "config.class.php";
require "config.php"
echo config::getItem(hash); // Geeft sha512 weer.
hash(config::getItem(hash), $pass . config::getItem(salt)); // Hashed $pass met onze config-hash + salt
?>
En dat was het dan weer.
Zoals altijd: Donaties zijn welkom, en laat de copyright graag staan. Ik ben overigens nu een boek over echt OOP programmeren aan het lezen, dus daarom geef ik een deel van mijn oude scripts vrij ^^
~ Rik
alleen snap ik er niks van

en netjes
Allen heb ik hem nu niet nodig, want ik gebruik CakePHP