SMF $user_info in as a class

I wrote up this method while writing my Pastebin. It was an experiment mostly to not have to use as many globals in my main code. I think it turned out nice and easy. Knowing that SMF 3.0 will use OOP does not help as this is the least likely way this would be implanted in the code. At least this can act as a bridge at that time to the new code.

I should explain the _() method. I set this up so I could do a Singleton. It also allows me to use it in a sorta static method by doing “userInfo::_()->id;”. Nice quick and easy.

I could of wrote this like my smcFunc class, but choose otherwise on the fact I wanted to use it as a object rather than a static method. A setup like that should be possible, but I didn’t test that.

I never tested but I don’t think accessing multiple dimensional parts of the $user_info will work. Things like $user_info[‘group’][1] most likely won’t work here. I should add support to drill down into the array, but will save that for a later day.

/*
* userInfo as a class.  We kinda do a poor method, but its the best way for now.
*/
class userInfo
{
	public static $instanceID = 0;

	public static function _()
	{
		if (self::$instanceID == 0)
			self::$instanceID = new userInfo;

		return self::$instanceID;
	}

	public function __set($key, $value)
	{
		global $user_info;
		$user_info[$key] = $value;
	}

	public function __get($key)
	{
		global $user_info;
		return isset($user_info[$key]) ? $user_info[$key] : null;
	}

	public function __isset($key)
	{
		global $user_info;
		return isset($user_info[$key]);
	}

	public function __unset($key)
	{
		global $user_info;
		unset($user_info[$key], $user_info[$key]);
	}
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.