Universally Accessible Data in PHP - Round 2

Posted under PHP on January 1, 2010.

This is another experiment with globally accessible data in PHP. This time I use a class with get/set methods for a slightly cleaner solution. It probably isn't as good as the singleton, but it works just fine for me.

Here's the class:

class data
{
	private static $x = array();
	
	public static function set($name,$val)
	{
		self::$x[$name] = $val;
	}
	
	public static function get($name)
	{
		return(self::$x[$name]);
	}
	
	public static function delete($name)
	{
		unset(self::$x[$name]);
	}
	
	public static function rename($old,$new)
	{
		self::$x[$new] = self::$x[$old];
		unset(self::$x[$old]);
	}
}

And you could use it like so:

function test()
{
	echo data::get('message');
}

data::set('message','This is awesome!');
test();

Stolen From

I don't come up with this stuff on my own:

Comment

See what others have to say on this topic, or add your own two cents.