Jump to content

Some advice on using SESSIONs


Shadowing

Recommended Posts

Was wondering if anyone had any thoughts on how im running settings on my text base game.

 

I currently have like 60 settings stored in the data base. These are only admin control settings. They set what things cost in the game and such.

 

I was thinking today that would it be better instead of running a query on every page to grab the settings that page uses. I just simply  do one huge select ALL query for the settings when a user logs in and turn all the setting into super global $_SESSION.

 

Also this way any function i made i can simply use the SESSION instead of grabing settings out side the function or creating a query for it with in the function.

 

So i was thinking doing this would extremly speed up my game wouldnt it?

wondering if anyone has any thoughts on this

 

 

 

Link to comment
Share on other sites

Only if you're already using sessions for something else (which I expect you are).

 

Also this way any function i made i can simply use the SESSION instead of grabing settings out side the function or creating a query for it with in the function.

Abstract that out. Use a Registry for all the settings, then have your code use that Registry. Then your code won't care about where the settings actually come from.

 

For the sake of making things easier, I'd forgo the usual pattern and just go with a bunch of static variables.

class Settings {

public static $SETTING1;
public static $SETTING2;
public static $SETTING3;

private function __construct() {}

public static function load() {
	if (!defined("SID")) session_start();

	if (empty($_SESSION["app settings"])) {
		// load settings from file and into $_SESSION
	}
	$s = $_SESSION["app settings"]; // shorthand
	Settings::$SETTING1 = $s["setting1"];
	Settings::$SETTING2 = $s["setting2"];
	Settings::$SETTING3 = $s["setting3"];
}

}

Settings::load();

Then Settings::$SETTING1 to grab a setting.

 

Since you're effectively caching the settings, I'd put in some mechanism for automatically reloading the settings in case they change. I suggest looking at the file modification time.

Link to comment
Share on other sites

  • 3 months later...

Alright 2 months later and looking back over what you said lol :)

 

really wish i understood this more

 

Now i kinda know what class's are

 

but i dont know what plubic static does

 

and i notice this line where you are creating a blank function?

private function __construct() {}

 

Is using :: some sort of syntax for class's?

 

oh boy

Link to comment
Share on other sites

but i dont know what plubic static does

 

Public static means that the visibility is "public", meaning you can access the object outside of the class itself, and the object is "static", which means you can access the object without having to instantiate the class.

 

and i notice this line where you are creating a blank function?

private function __construct() {}

 

Since the class uses all static objects, there is no reason to instantiate it - setting the constructor to private means that it cannot be instantiated and will throw an error if you try.

 

Is using :: some sort of syntax for class's?

 

:: is the Scope Resolution Operator. It is essentially the equivalent to the -> operator for static objects.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.