<?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');
		}
	}
	
}


?>

[b]Voorbeeld[/b]
<?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();



?>




[b]Nieuwe simpelere versie:[/b]
<?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');	
    }    
}
?>


