<?php
/*
ValueHolder Class v2

Creation Notes:
Made by Chris Rogers
Made on 09/23/2010
Version 2:
Changed variable names.
Changed function names.
Properly commented entire script.
Changed the methods in-which functions work.

Description:
Creates an easy method to store an array of variables directly into one variable.

Functions:
__construct ( )
__get ( $Name )
__set ( $Name , $Value )
load_array ( $Array )
*/
class ValueHolder {
public $Vars; // Holds all values

public function __construct ( ) {
// Initilize Vars variable
$this->Vars = Array ( );
}

public function __get ( $Name ) {
// Check if value is set and then return it, if it's not return null
return ( isset ( $this->Vars[$Name] ) ? $this->Vars[$Name] : null );
}

public function __set ( $Name , $Value ) {
// Set the value in Vars
$this->Vars[$Name] = $Value;
}

public function load_array ( $Array ) {
// Make sure it's an array
if ( is_array ( $Array ) ) {
// Load in the values
foreach ( $Array as $Name => $Value ) {
// Set the value
$this->$Name = $Value;
}
}
}
}
?>


This is a really simple and great class for handling obscure data such as; Settings, Options, Specific Information.

I commonly use this inside configuration scripts to handle settings, members, member information, icons, images, urls, and much much more.

Here is a quick example of how to use this script;
<?php
// Require valueholder class file.
require_once ( "valueholder.php" );

// Should we array load, or single load?
$Single = true;

// Create person valueholder.
$Person = new ValueHolder();

if ( $Single == true ) {
// Add valuable data in single form.
$Person->name = "Chris";
$Person->age = 16;
$Person->sex = 0;
} else {
// Add valuable data in array form.
$Person->load_array ( Array (
'name' => "Chris",
'age' => 16,
'sex' => 0,
) );
}

echo $Person->name . " is " . $Person->age . " years old, and is a " . ( $Person->sex == 0 ? "male" : "female" ) . ".";
?>