constante-arrayobject
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
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
73
74
75
76
77
78
79
80
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
73
74
75
76
77
78
79
80
<?php
/**
* With this class you can put array and objects in constants.
*
*/
class serConst {
const PREFIX = 'serConst_';
const SER_PREFIX = 'SERIALIZED|';
/**
* Set the array/object constant
*
* @param string $constant_name
* @param array/object $array
*/
public static function set($constant_name,$value){
if(!empty($constant_name)){
if(!defined($constant_name)){
if(!empty($value)){
try {
if(in_array(gettype($value),array('float','array','object'))){
define(self::PREFIX.$constant_name,self::SER_PREFIX.serialize($value));
}else{
define(self::PREFIX.$constant_name,$value);
}
define($constant_name,$constant_name);
}catch(Exception $e){
throw new Exception('serConst Error: Unknown error');
}
}else{
throw new Exception('serConst Error: The given value is empty');
}
}else{
throw new Exception('serConst Error:The constant name is already in use');
}
}else{
throw new Exception('serConst Error:The constant name is not set');
}
}
/**
* Get the constant array
*
* @param string $constant
* @param string $key
* $param string $keySeperator
*/
public static function get($constant,$key='',$keySeperator=','){
if(!empty($constant)){
try {
$value = constant(self::PREFIX.$constant);
if(substr($value,0,strlen(self::SER_PREFIX)) == self::SER_PREFIX){
$value = unserialize(substr($value,strlen(self::SER_PREFIX)));
}
if(!empty($key) and is_array($value)){
$arrKey = explode($keySeperator,$key);
foreach($arrKey as $key){
if(isset($value[$key])){
$value = $value[$key];
}else{
throw new Exception('serConst Error: this array key: '.$key.' does not exists');
}
}
}
return $value;
}catch(Exeption $e){
throw new Exception('serConst Error: Unknown error');
}
}else{
throw new Exception('serConst Error:The constant name is not set');
}
}
}
?>
/**
* With this class you can put array and objects in constants.
*
*/
class serConst {
const PREFIX = 'serConst_';
const SER_PREFIX = 'SERIALIZED|';
/**
* Set the array/object constant
*
* @param string $constant_name
* @param array/object $array
*/
public static function set($constant_name,$value){
if(!empty($constant_name)){
if(!defined($constant_name)){
if(!empty($value)){
try {
if(in_array(gettype($value),array('float','array','object'))){
define(self::PREFIX.$constant_name,self::SER_PREFIX.serialize($value));
}else{
define(self::PREFIX.$constant_name,$value);
}
define($constant_name,$constant_name);
}catch(Exception $e){
throw new Exception('serConst Error: Unknown error');
}
}else{
throw new Exception('serConst Error: The given value is empty');
}
}else{
throw new Exception('serConst Error:The constant name is already in use');
}
}else{
throw new Exception('serConst Error:The constant name is not set');
}
}
/**
* Get the constant array
*
* @param string $constant
* @param string $key
* $param string $keySeperator
*/
public static function get($constant,$key='',$keySeperator=','){
if(!empty($constant)){
try {
$value = constant(self::PREFIX.$constant);
if(substr($value,0,strlen(self::SER_PREFIX)) == self::SER_PREFIX){
$value = unserialize(substr($value,strlen(self::SER_PREFIX)));
}
if(!empty($key) and is_array($value)){
$arrKey = explode($keySeperator,$key);
foreach($arrKey as $key){
if(isset($value[$key])){
$value = $value[$key];
}else{
throw new Exception('serConst Error: this array key: '.$key.' does not exists');
}
}
}
return $value;
}catch(Exeption $e){
throw new Exception('serConst Error: Unknown error');
}
}else{
throw new Exception('serConst Error:The constant name is not set');
}
}
}
?>
Voorbeeld
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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
include_once 'serConst.php';
/**
* A quick way to print an array or object with print_r()
*
* @param mixed $var
*/
function print_rpre($var){
echo '<pre>';
print_r($var);
echo '</pre>';
}
// Lets make an array
$myArray= array('This is','an array',array('which','is even multidimensional'),'and'=> 'Now we have an array.');
// First we print the array
print_rpre($myArray);
// Now, we are going to put the array into the constant.
try {
serConst::set('MY_ARRAY',$myArray);
}catch(Exception $e){
echo $e;
}
// To get the array back, you can use the function 'serConst::get()'
try {
print_rpre(serConst::get(MY_ARRAY));
}catch(Exception $e){
echo $e;
}
// If the constant is an array, you can read the values using the 2nd paramter like this.
try {
// to get the equivalent out put of $myArray[2][1]
echo serConst::get(MY_ARRAY, '2,1');
echo '<br />';
// to get the equivalent out put of $myArray['and']
echo serConst::get(MY_ARRAY, 'and');
echo '<br />';
// You can also change the seperator by using the 3th parameter
echo serConst::get(MY_ARRAY, '2;1',';');
}catch(Exception $e){
echo $e;
}
// You can also use objects
class foo{
public $bla = 'hoi';
function __construct(){
print_rpre(serConst::get(MY_ARRAY));
}
}
$bar = new foo;
$bar = $bar->bla;
try {
serConst::set('OBJECT_FOO',$bar);
print_rpre(serConst::get(OBJECT_FOO));
}catch(Exception $e){
echo $e;
}
// You can even use string, integer or someting else
serConst::set('Hallo','Hoe is tie dan ??');
echo serConst::get(Hallo);
function bla(){
print_rpre(serConst::get(MY_ARRAY));
}
bla();
?>
include_once 'serConst.php';
/**
* A quick way to print an array or object with print_r()
*
* @param mixed $var
*/
function print_rpre($var){
echo '<pre>';
print_r($var);
echo '</pre>';
}
// Lets make an array
$myArray= array('This is','an array',array('which','is even multidimensional'),'and'=> 'Now we have an array.');
// First we print the array
print_rpre($myArray);
// Now, we are going to put the array into the constant.
try {
serConst::set('MY_ARRAY',$myArray);
}catch(Exception $e){
echo $e;
}
// To get the array back, you can use the function 'serConst::get()'
try {
print_rpre(serConst::get(MY_ARRAY));
}catch(Exception $e){
echo $e;
}
// If the constant is an array, you can read the values using the 2nd paramter like this.
try {
// to get the equivalent out put of $myArray[2][1]
echo serConst::get(MY_ARRAY, '2,1');
echo '<br />';
// to get the equivalent out put of $myArray['and']
echo serConst::get(MY_ARRAY, 'and');
echo '<br />';
// You can also change the seperator by using the 3th parameter
echo serConst::get(MY_ARRAY, '2;1',';');
}catch(Exception $e){
echo $e;
}
// You can also use objects
class foo{
public $bla = 'hoi';
function __construct(){
print_rpre(serConst::get(MY_ARRAY));
}
}
$bar = new foo;
$bar = $bar->bla;
try {
serConst::set('OBJECT_FOO',$bar);
print_rpre(serConst::get(OBJECT_FOO));
}catch(Exception $e){
echo $e;
}
// You can even use string, integer or someting else
serConst::set('Hallo','Hoe is tie dan ??');
echo serConst::get(Hallo);
function bla(){
print_rpre(serConst::get(MY_ARRAY));
}
bla();
?>
Nieuwe simpelere versie:
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
27
28
29
30
31
32
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
<?php
/**
* With this class you can put array and objects in constants.
*
*/
class serConst {
static public $store = array();
/**
* Set the array/object constant
*
* @param string $constant_name
* @param array/object $array
*/
public static function set($constant_name,$value){
self::$store[$constant_name] = $value;
}
/**
* Get the constant array
*
* @param string $constant
*/
public static function get($constant){
if(isset(self::$store[$constant])){
return self::$store[$constant];
}
throw new Exception('This key does not exists');
}
}
?>
/**
* With this class you can put array and objects in constants.
*
*/
class serConst {
static public $store = array();
/**
* Set the array/object constant
*
* @param string $constant_name
* @param array/object $array
*/
public static function set($constant_name,$value){
self::$store[$constant_name] = $value;
}
/**
* Get the constant array
*
* @param string $constant
*/
public static function get($constant){
if(isset(self::$store[$constant])){
return self::$store[$constant];
}
throw new Exception('This key does not exists');
}
}
?>